An array is nothing more than a collection of values which can be accessed by the same variable name. This is helpful in storing large amounts of related data. The simplest way to understand the concept of an array is to think of a list of values. The name of the list is your array's variable name. A simple string declaration in Ruby would look like this:
myString = "This is my string"
However, an array containing some string elements would be declared like this,
myArray = ['This', 'is', 'my', 'code']
Since Ruby is an object-oriented language, abstract data types, like arrays, are actually classes. Methods related to arrays are defined in the Array class and is a part Ruby's core language. Hence, all distributions of Ruby will contain this class. An Array object is created using the new method. Below is an example of doing the same.
myArray = Array.new
Remember that if you've already declared the array values like in the first example, you do not need to create the array using the new method. The new method will create only an empty array with no values. Another way to create an array and initialize its values:
myArray = Array.[](1,2,3)
Individual values of an array are accessed by their indexes. An index is a numerical subscript which denotes the element number. For instance, in our last example - the element '1' will have an index of 0. The element '2' will have an index of 1 and so on. Notice that the index values start from 0 and not 1 as is the case with quite a few other languages. Thus, to access the element of an array and do an operation on it, we need to simply provide its index. The code shown below creates a new array and prints out the second element using the puts method.
likeWords = ["first", "second", "third"]
puts likeWords[1]
One of the most common operations that are used on arrays is inserting and removing data. When we want to add data at the end of the array, we use the push method, and when we want to remove the last element of the array we use the pop method. Ruby allows us to treat the array like a stack, a data structure where insertions and deletions can only be done at one end of the array. Have a look at the code below to understand how these two methods are used:
# Array Declaration
likeWords = ["first", "second", "third"]
puts likeWords.length
puts likeWords[3]
# Adding an element
likeWords.push "fourth"
puts likeWords.length
puts likeWords[3]
# Removing the last element
likeWords.pop
puts likeWords.length
puts likeWords[3]
Output
-------
3
nil
4
fourth
3
nil
In the above example, the length of the array when created is 3, then becomes 4 after pushing another element, and then becomes 3 again after we pop the element we just pushed. Notice that when we try to access the fourth element of the array (the 3rd index), the resultant output is 'nil'. If you want to find the number of elements an array holds, you can use the length or size method. The latter is just an alias of the length method and will give you the exact same output. The code below prints out the number of elements an array called 'likeWords' contains.
puts likeWords.length
On similar lines, the last method simply gives you access to the last element of the array without deleting or changing it in any way. The syntax for printing out the last element of an array is given below.
puts likeWords.last
This wraps up our discussion of Ruby arrays. There are many more methods that are defined for the Array class but this should suffice as an introduction. For further reference, The Ruby Way by Hal Fulton is a nice book though not intended for novices. If you're just starting out with Ruby, Learn to Program by Chris Pine is an excellent introduction
Written by Rae |