This week I decided to learn a bit of PHP. Why? - for a few reasons but mainly to continue on the path of self-learning. From what I have read on tech blogs it is beginner-friendly and has a gradual path for improvement as you get more experienced with it. Also, I enjoyed the Object Oriented Programming aspect of Ruby so I thought I should give it a go.
I’ve mostly been picking up on entry level code this week, I am following along a Free Code Camp tutorial to get an overall view quickly so I can move onto the more interesting parts of what the language can do.
The set up was fairly straightforward following the docs on the PHP website and so far I’m using the CLI with its built in web server and Sublime as the IDE to get started.
The links I used can be found here:
- PHP getting started: https://www.php.net/manual/en/getting-started.php
- PHP and the built-in web server: https://www.php.net/manual/en/features.commandline.webserver.php
- Free Code Camp tutorial: https://www.youtube.com/watch?v=OK_JCtrrv-c
Working through Exercism has some great exercises to brush up on coding skills. This particular challenge involved converting a phrase to its acronym. The coding challenge is to make the failing tests pass by writing the corresponding code. The first test was as follows:
Firstly the problem needed to be broken down so I began with some basic instructions to myself:
- Convert String to an Array of Strings (i.e. each of the words in a separate String)
- Convert the Array of Strings to an Array of Arrays (i.e. each of the words in a separate Array)
- Break each of these Arrays (of Arrays) so only the first letter of each array (word) is returned.
- Join the Array of Arrays into a single String i.e. the Acronym.
The code was as follows. The words following ‘//’ is the output in the irb (interactive ruby console).
Prior to this exercise I wasn’t aware of the each_slice method in Ruby which allows you to split an Array of Strings into an Array of sub-Arrays. It was a question in Stack Overflow that lead me to this discovery. There were also a few lessons in syntax such as for the ‘.join’ method which took a bit of playing around in irb to eventually realise simplicity was the answer. I initially also thought the final answer would require converting an array into a string to return the acronym however Ruby does this for you thus not requiring such a step.
I am still working through the rest of this problem so I will be refactoring this solution into better naming patterns, shortening it where possible and including support for lower case characters, punctuations, all caps words etc. as part of the challenge - more to come as I work through the challenge.