Arrays on Ruby | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Arrays on Ruby

Can you explain use puts [-1] in array?

24th Feb 2022, 5:41 AM
Leydy Mayumy
Leydy Mayumy  - avatar
1 Answer
+ 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
24th Feb 2022, 6:08 AM
Simba
Simba - avatar