I find myself wanting something like Python's
ary = [1,2,3,4,5,6,7,8]
ary[2:] #=> [3,4,5,6,7,8]
ALL of the time these days.
The solution always ends up being multi-lined and ugly. I'm wondering what the most elegant solutions out there might be, because mine are not worth showing.
Use Array#drop
2.1.0 :019 > ary.drop(2)
=> [3, 4, 5, 6, 7, 8]
You can write:
ary[2..-1]
# => [3,4,5,6,7,8]
-1
is the index of the last element in the array, see the doc for Array#[]
for more informations.
A better alternative in Ruby is to use the Array#drop
method:
ary.drop(2)
# => [3,4,5,6,7,8]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With