+ 1
Arrays on Ruby
Can you explain use puts [-1] in array?
1 Resposta
+ 4
Like python, the negative indexing starts from where the array ends in ruby. It means that the index value of -1 gives the last element, and -2 gives the second last element of an array.
For example,
An array contains 4 elements,
a = [1,2,3,4]
a[-1] will print the last element 4
a[-2] indicate the 2nd last element 3.. and so on