Search⌘ K

Arrays of Arrays: Two-dimensional arrays

Explore how to initialize and work with arrays of arrays in Ruby, focusing on two-dimensional structures. Understand why modifying one nested array affects others due to object references, and learn to avoid common beginner mistakes.

We'll cover the following...

Initializing different types of arrays

We can specify any type while initializing arrays. For example, string:

$ irb
> Array.new(10, 'hello')
=> ["hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello"]

Or Boolean:

Note: Boolean type doesn’t exist in Ruby but, in this course, we intentionally refer to both theTrueClass and FalseClass types as Boolean.

$ irb
> Array.new(10, true)
=> [true, true, true, true, true, true, true, true, true, true]

Or integer:

$ irb
> Array.new(10, 123)
=> [123, 123, 123, 123, 123, 123, 123, 123, 123, 123]

In other words, ...