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 - Documentation for Ruby 3.0.0

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs