Quick tip today on using negative indexes on Ruby arrays

array = [:a, :b, :c, :d, :e]
 
array[-1] 
=> :e
 
array[0..-3] 
=> [:a, :b, :c]
 
array[1..-1]
=> [:b, :c, :d, :e]
 
>> array[1.-1] 
=> :a

Leave a Reply

Your email address will not be published. Required fields are marked *