Tuesday, February 4, 2014

Ruby Puzzles Part One of n




With a slew of technical interviews on the horizon, I figured it would be worth the time to work through a series of basic ruby concepts and coding problems and Basic array manipulation in Ruby

stuff = [:dog,:cat,:orange,:banana]
How can you slice this array to create a new array [:cat,:orange]



    This can be done in a few different ways, however the most direct is to use the slice method, which is shown here. The slice method takes two parameters. The first is the index you would like to start at, and the second is the number of elements you would like to slice out of the array.



Add the element :apple on to the end of the array.
   Now take :apple back off again
   Add the element :fish to the start of the array.
   Now remove the element :fish.


     For this step I'll use the .push , .pop, .unshift and .shift methods. The .push method takes a parameter and adds it to the end of the array. The converse of that is .pop. It removes the last item of the array and returns it. The .unshift and .shift methods work in the very same why, except that they preform the actions on the beginning of the array.

No comments:

Post a Comment