As a novice programmer, I am constantly reminded of how I felt when training to play sports. To me, playing around with primitive objects such as arrays and strings is very much like playing wall ball in any sport. It is repetition of a core skill set.
Outside of the transformative enumerable methods such as
.map, arrays are responsive to a variety of methods that allow us to sort and manipulate them very quickly without passing the method a block.
As we add elements on top of an array’s stack with the push method. The items are ordered based on the sequence in which we pushed them onto the stack.
1 2 3
In order to get a sorted copy of the array returned to us, we simply would need to type.
but what if we wanted them sorted in a descending order. We can use a little method chaining here to produce the desired effect.
Often times we are passed a string or array and we need to break it apart at a chosen delimiter. The
.chars method allows us to break apart a string into the individual characters. The return of this is an array of the individual characters.
Now that we have returned an array of each indiviudal character in the string, we may want to work with the new data but because we used the
.chars method we also have
" " character in our return array.
We have several options here such as using a
.reject method call but I am going to touch on a simple way of doing this that I have used. We can subtract the characters we don’t want from the array. This works for any character as well and does not modify the reciever.
There are many more basic work flows that I find myself repeating through out the semeester and it has helped me to be able to just run them quickly when I have some free time so that when presented with a task, it is second nature to move through a given work flow.
Next post I am going to go over a few simple ways to convert arrays into hashes with some concise and simple iterations.