tags:

views:

65

answers:

3

how can I access all array elements from x to the last one?

my_array= [1,2,3,4,5,6]
puts my_array[3..last]
+6  A: 

An index of -1 gives the last item in the array:

my_array[3..-1]

In fact, any negative index begins counting backwards from the end of the array.

Thanks to Peter for reminding me of the better way to do this.

Aaron
@Aaron: thank you, works nicely
Radek
@Peter: My apologies; I edited in a hurry. Credit given.
Aaron
+6  A: 

Use a negative index, as in my_array[3..-1].

my_array= [1,2,3,4,5,6]
puts my_array[3..-1]
=> [4, 5, 6]
Peter
+1 cause you thought of it :)
Earlz
A: 

also you can use range and inspect. like this

puts my_array[3..6].inspect
mgpyone