Friday, March 21, 2014

Alternative to using take and drop methods in Array class

The take method takes elements from the beginning whereas the drop takes the elements from the end of the array. These methods require some mental mapping of the method names to what it does. I was thinking about reducing this gap. Here is the experiment in the IRB:

2.0.0 > a = [1,2,3,4]
 => [1, 2, 3, 4]
2.0.0 > a.first(2)
 => [1, 2]
2.0.0 > a
 => [1, 2, 3, 4]
2.0.0 > a.last(2)
 => [3, 4]
2.0.0 > a
 => [1, 2, 3, 4]

It just works! This is one of the reason why I love Ruby.