Ruby Surprise B1 - You can use negative indexes
If I use a negative index as the index to your array, it works, but you count from the right hand end, rather than the left:
Ruby Surprise B2 - No ArrayIndexOutOfBoundsException
If I blow the bounds on my array, I just get a nil, rather than an ArrayIndexOutOfBoundsException equivalent:
Ruby Surprise B3 - Addressing Ranges is Easy
There are a number of ways to get a range of values from my array. The first is to pass in two arguments, the start index and the range size:
Then there is the start index and the end index version...:
Or the start index and the one before the end index version...:
And I can even use the negative indexing paradigm as mentioned in Surprise B1...:
Ruby Dissapointment 1 - You can't Count Backwards Through an Array (but Groovy can)
This works in Groovy (which I am learning from a colleague has a very similar syntax to Ruby) but not Ruby:
1 comment:
There's always a way (maybe not "out of the box"):
a[-6, 6].reverse
~OR~
a.reverse.inject { |mystr, elem| mystr + elem }
Post a Comment