FAQ: Data Structures - Arrays of Arrays

This community-built FAQ covers the “Arrays of Arrays” exercise from the lesson “Data Structures”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Arrays of Arrays

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why is the multidimensional array 2 dimensional? There are 4 arrays within the example multidimensional array no?

The first thng we need to appreciate about arrays is their linearity. Arrays are linear in nature.

When the elements of an array are themselves arrays, it has no bearing on the linearity of the parent array. It’s an array of arrays. Arrays are not intended to emulate tables so there is no x by y dimensioning in their definition.

Hi everyone,
what does the “#” sign job in the code,
and how come you can use x and n
without declaring them
regarding this code

multi_d_array.each { |x| puts “#{x}\n” }

2 Likes

The # when paired up with {} signifies expression interpolation. Notice in your code it is written,

puts "#{x}\n"

which literally means,

put string comprised of an expression containing the current value of x and a newline escape character.

3 Likes

thank you for your reply, and how did the “n” affected the output, and again without declaring it before

1 Like

Written as an escape sequence, \n inserts a newline character (line break) which when added to the one that puts inserts gives double line spacing.

3 Likes

multi_d_array = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]

multi_d_array.each { |x| puts “#{x}\n” }

Hi, could somebody please explain why we use { |x| puts “#{x}\n” }. I am a little confused

The Array.each method takes a block, the { } which encloses the code that will run on each iteration of the array. |x| is called the block parameter, a variable local to the block which binds to one element in the array at a time. In the above, we puts that value to the display. "#{x}\n" is a string which employs interpolation of the variable’s value along with a newline character which when combined with the one Ruby inserts after every puts gives a blank line between each print out. puts is short for, put string.

Expected output

[0, 0, 0, 0]

[0, 0, 0, 0]

[0, 0, 0, 0]

[0, 0, 0, 0]

1 Like

why do I need to type
puts x
instead of
puts “#{x}\n”

If the puts automatically adds a newline anyway, isn’t the extra code unnecessary? Does it mean something that I am not advanced enough to understand yet?