What are negative indexes and why are they used?

Hi, i need to know What are negative indexes and why are they used

What are negative indexes and why are they used

For “what” they are, see the documentation: class Array - RDoc Documentation

A negative index is an offset, backwards, from the end of the array:

  • Index -1 indicates the last element.
  • Index -2 indicates the next-to-last element.

“Why” can only be answered for a specific piece of code - the programmer would find this language feature solves their problem better than another.