Lesson 1
Multidimensional Arrays and Their Traversal in Ruby
Topic Overview

Welcome to today's session on "Multidimensional Arrays and Their Traversal in Ruby". Multidimensional arrays are types of arrays that store arrays at each index instead of single elements. Picture it as an 'apartment building' with floors (the outer array) and apartments on each floor (the inner array). Our goal today is to strengthen your foundational knowledge of these 'apartment buildings' and how to handle them effectively in Ruby.

Creating Multidimensional Arrays

To construct a multidimensional or nested array in Ruby, we use arrays inside arrays. Here's an example of a 2-dimensional array:

Ruby
1# Creating a 2D array 2array = [[1, 2, 3], 3 [4, 5, 6], 4 [7, 8, 9]] 5puts array.inspect

In this example, array is a 2-dimensional array, just like a 3-story 'apartment building,' where every floor is an inner array.

Indexing in Multidimensional Arrays

All indices in Ruby arrays are 0-based. Let's say you want to visit an apartment on the second floor (index 1) and bring a package to the first unit (index 0) in this building. Here's how you can do it:

Ruby
1# Accessing an element 2puts array[1][0] # Outputs: 4

We visited the element 4 in the array by its position. The number 1 inside the first square brackets refers to the second inner array, and 0 refers to the first element of that array.

Updating Multidimensional Arrays

Continuing with the apartment-building analogy, suppose the task was to replace the old locker code (the second element in the first list) with a new one. Here's how we can achieve this:

Ruby
1# Updating an element 2array[0][1] = 'New Code' 3puts array.inspect
Common Built-in Methods

Ruby offers a variety of built-in methods that are handy with multidimensional arrays:

  1. length gives the number of elements in the outer array (the number of floors). Note that It does not account for how many elements are inside each nested array (apartments). For example:
    Ruby
    1array = [[1, 2], [3, 4, 5], [6]] 2puts array.length # Outputs: 3 (three outer elements) 3puts array[1].length # Outputs: 3 (three inner elements in the second array)
  2. push: With push, we can add a new floor and units on that floor to our 'apartment building.'
    Ruby
    1# Adding a new row to our array 2array.push(['Unit-1', 'Unit-2', 'Unit-3']) 3puts array.inspect
  3. delete: We can rely on delete to help us get rid of a specific element in our 'apartment building.'
    Ruby
    1# Removing an element 2array[1].delete('New Code') 3puts array.inspect
Traversing Multidimensional Arrays

We can visit every floor (outer array) and every apartment on each floor (inner array) by using nested loops.

Ruby
1array = [["Apt 101", "Apt 102", "Apt 103"], 2 ["Apt 201", "Exit Floor", "Apt 203"], 3 ["Apt 301", "Apt 302", "Apt 303"]] 4# Loop through 2D array 5array.each do |floor| 6 floor.each do |unit| 7 print "#{unit}, " 8 end 9 puts 10end

This code will print:

1Apt 101, Apt 102, Apt 103, 2Apt 201, Exit Floor, Apt 203, 3Apt 301, Apt 302, Apt 303,
Break/Next in Nested Loops

Sometimes, when we visit every apartment on each floor, we might need to start visiting the next floor midway. break helps us exit the current loop, while next helps us skip the current iteration and move to the next one.

Ruby
1# Break in nested loop 2array.each do |floor| 3 floor.each do |unit| 4 break if unit == 'Exit Floor' 5 print "#{unit}, " 6 end 7 puts 8end

This code will print:

1Apt 101, Apt 102, Apt 103, 2Apt 201, 3Apt 301, Apt 302, Apt 303,

Here, as soon as 'Exit Floor' is found on a floor, the inner loop breaks, and no further units on the floor are visited. However, the further units on other floors are processed as before because break only exits the inner loop.

We can also make use of next in a similar scenario:

Ruby
1# Next in nested loop 2array.each do |floor| 3 floor.each do |unit| 4 next if unit == 'Exit Floor' 5 print "#{unit}, " 6 end 7 puts 8end

This code will print:

1Apt 101, Apt 102, Apt 103, 2Apt 201, Apt 203, 3Apt 301, Apt 302, Apt 303,

In this case, when 'Exit Floor' is encountered, the next statement is executed, skipping printing 'Exit Floor' and continuing with the next unit in the same floor.

Lesson Summary and Practice

That was exciting! We went through various operations on multidimensional arrays, starting from their creation, methods to update, and useful built-in methods. We also learned how we can visit every floor and every apartment on each floor.

Practice solidifies learning! Your new adventure awaits in our upcoming practical exercises, where you can apply these concepts on multidimensional arrays! Buckle up and have fun!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.