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:

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:

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:

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:
  2. push: With push, we can add a new floor and units on that floor to our 'apartment building.'
  3. delete: We can rely on delete to help us get rid of a specific element in our 'apartment building.'
Traversing Multidimensional Arrays

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

This code will print:

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.

This code will print:

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:

This code will print:

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal