Ruby:Accessing an Array

From Progzoo
Jump to navigation Jump to search

You access an array using the square bracket notation.

The first element of array x is x[0]


[Font] [Default] [Show] [Resize] [History] [Profile]

The last element of array x is x[-1]

Negative numbers indicate index from the end of the array:


[Font] [Default] [Show] [Resize] [History] [Profile]

Out of Bounds

If you try to read past the end of an array it simply returns nil.


[Font] [Default] [Show] [Resize] [History] [Profile]