In Ruby, (unlike many other languages) a single Array
can hold items of mixed data types such as strings,
integers and floats or even a method-call
which returns some value:
a1 = [1,'two', 3.0, array_length(a0)]
The first item in an array has the index 0, which means that the final item has an index equal to the total number of items in the array minus 1. Given the following array, this is how to obtain the values of the first and last items:
a1[0] # returns 1st item (at index 0) a1[3] # returns 4th item (at index 3)
Creating Arrays
In common with many other programming languages,
Ruby uses square brackets to delimit an array.
You can easily create an array, fill it with some
comma-delimited values and assign it to a variable:
arr = ['one','two','three','four']
Arrays are...
You can reference an item in an array by placing its index between square brackets. If the index is invalid, nil is returned: