Ruby: Arrays
An array is a data-type in Ruby that can be looked at as a type of container. The container holds elements. These elements can be numbers, booleans, symbols, strings, even hashes or other arrays! Arrays are recognised by elements seperated by commas within square brackets. For instance:
wacky_array = [6, "hello", false, [1, 2], :colour]
Because arrays are containers with several elements (or if not, they are containers that have the ability to store several elements) the naming convention for an array is to make it either plural or a collective noun, and also to be as specific as possibly for clarity. For instance:
cat = ["Fluffy", "Mog", "Ginger"]
doesn’t make much sense, compared to
cat_names = ["Fluffy", "Mog", "Ginger"]
Creating an Array
Creating an array could be achieved by simply writing its name assigned to its elements in square brackets as above, or a new empty array could be created like so:
cat_names = Array.new
or
cat_names = []
Then elements can be added to this array afterwards. This might be useful when we are taking information from an existing array, changing each element and then want to return it. We could put each edited element into this new array, rather than permanently changing the original. Array.new
means that we are creating a new instance of the array class. When we are declaring a class in Ruby we must capitalise it, so Ruby understands that is what it is.
Adding Elements to an Array
Elements can be added to an array most simply using the shovel operator, denoted as <<
. The shovel operator takes an element and shovels it into the array at its current last position. It is used like so:
Multiple objects can be shoveled into an array at one time, like so:
Accessing Elements Within an Array
Each element in an array has an index position, a numbered position indicating where in the array it sits. The first position is [0]
and continues consecutively. For instance in array_example = [1, 3, 6, 2, 5, 4]
, the element at index position [1]
is the number 3
. The last position in an array is denoted as [-1]
. In order to access these array elements based on their index position we can return them as array_name[index_position]
, like so:
or
So this shows you how to find the element at a given index position, but what if you want to find the index position of a certain element? This can be achieved with the #index
method. It works like so:
If I were to call favourite_animals.index("earthworm")
, then nil
would be returned, as that element does not exist in the given array.
Multidimensional Arrays
As said before, an array can contain other arrays inside it. For instance:
array_of_arrays = [[1, 2], [2, 3], [3, 4]]
When accessing this information by index position, array_of_arrays[1]
returns [2, 3]
, but if we want to access the 3
within that, we could access it like so:
Some Common Array Methods
The #index
method shown above is an example of a Ruby method that acts on an array. Here are some others that are very useful:
#length
or #size
returns the number of elements within the array.
#reverse
reverses the array.
#sort
puts the array in ordered numbers or alphabetical order
#pop
removes an element from the end of an array and returns it.
#push
adds an element to the end of an array.
#uniq
removes multiple instances of the same element from an array.
#delete
deletes a specified element.
#join
joins all elements and returns as a string.
#include?
returns a boolean value as to whether the given element exists in that array.