Ruby Multidimensional Arrays Tutorial

  • Post author:
  • Post category:Ruby

In this section, we will learn what the multidimensional array is and how to use it in Ruby.

Note: we’re assuming you’re already familiar with the Ruby arrays.

What is a Multidimensional Array in Ruby?

A multidimensional array is an array that has other arrays set as its elements.

How to create a multidimensional array in Ruby?

Creating a multidimensional array in Ruby is really simple! All you need to do is to set new arrays for the indexes of the target array.

Example: creating a multidimensional array in Ruby

arr = Array.new

arr[0] = [1,2,3,4,5,6]

arr[1] = ["One","Two","Three"]

puts arr.to_s

Output:

[[1, 2, 3, 4, 5, 6], ["One", "Two", "Three"]]

As you can see, the `arr` array has two elements and they are themselves other arrays!

Ruby Multidimensional Arrays: Accessing Elements

There’s no difference on the way we access the elements of a multidimensional array or a simple array! Basically, we still use the index number set in a pair of bracket to access the target element in the array.

The only thing to remember now is that you get an array as a result of calling an index of an array!

In the simple program of the example above, if we call the `arr` to get its first element like `arr[0]` for example, we will get an array as a result!

But remember, you’ll get a reference and not a copy to the sub-array! This means if you try to change the array, the change will be seen in the array as well.

Example: accessing the elements of a multidimensional array in Ruby

arr = Array.new

arr[0] = [1,2,3,4,5,6]

arr[1] = ["One","Two","Three"]

arr[0][1]=100

puts arr.to_s

Output:

[[1, 100, 3, 4, 5, 6], ["One", "Two", "Three"]]

Note how we accessed the second element of the sub-array that is set as the first element of the arr array:

arr[0][1]

This is actually two statements:

The first one is:

arr[0]

Which will return the first element of the `arr` array (which itself is another array).

Now the second call:

[1]

Is telling that we want to work with the second element of the returned array!

That’s how we get access to the elements of sub-arrays and change their values.

Ruby Multidimensional Arrays: Getting the length

We mentioned in the Ruby array section that we can call the `length` method in order to get the length (number of elements) of an array!

This is still the same when it comes to multidimensional array though! Basically, when we call the method on an array, it doesn’t care if the elements of the array are themselves another array or not! The length method just returns the number of elements the target array has!

Example: getting the length of a multidimensional array

arr = Array.new

arr[0] = [1,2,3,4,5,6]

arr[1] = ["One","Two","Three"]

puts "The length of the array is: #{arr.length}"

Output:

The length of the array is: 2

Ruby Multidimensional Arrays: Loops and Iteration

We can use loops like while loop and for loop to iterate through the elements of sub-arrays of a multidimensional array.

The important thing to remember is that you need a loop like while or for per each sub-array.

For example, if your array is a two-dimensional array (AKA 2D Array), then you would need two nested loops in order to get to the inner elements of the sub-array.

Basically, the first loop will return the elements of the first array (parent array if you will) and the second loop will get you the inner elements of the sub-array.

Example: iterating through the elements of a multidimensional array

arr = Array.new 

arr[0] = [1,2,3,4,5,6]
arr[1] = ["One","Two","Three"]

for pElement in arr do 
    for iElement in pElement do 
        puts iElement
    end 
end 

Output:

1

2

3

4

5

6

One

Two

Three

Here the first for-loop returned the elements of the `arr` array and then we used the result of this for-loop (the pElement which represents each element of the parent array) and used it as the collection of the inner for loop. Now within the body of the inner for loop, we could access the elements of the sub-arrays in the `arr` array.

Leave a Reply