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]
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]
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.
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]
also you can use range and inspect. like this
puts my_array[3..6].inspect