Learn Ruby

Get item from a position

We have an array called items. It has 4 items.

items = ["ice cream", "chocolate", "movies", "beaches"]

If we want second item from the array then we need to do items[1]. In computer science the first position is 0 and not 1.

If we want third item then we need to do [2].

You can see the pattern. For the 4th item we need to do [3]. It means that the first item of an array starts at zero and not at one.