Search⌘ K

Initializing Arrays

Explore how to initialize simple one-dimensional and two-dimensional arrays in Ruby, understand differences between empty arrays and nil values, and practice iteration techniques using built-in methods. This lesson helps learners build a foundation for managing array data structures effectively in Ruby.

In this lesson, we’ll learn how to initialize a simple, one-dimensional array. There could be arrays with multiple dimensions, but for beginner programmers, two dimensions are usually enough. We will make sure to practice initializing, accessing, and iterating over 1D and 2D arrays, because it’s a very common pitfall during interviews. Sometimes, initializing arrays correctly is winning half the battle.

We are already familiar with the following syntax, which creates an empty array:

arr = []

Array with predefined values

If we want to initialize an array with ...