Ad space (banner)
๐Ÿ’ŽRuby Lessons
Lesson 24 / 68

Two-Dimensional Arrays (Tabular Data)

In this lesson you will learn how to work with two-dimensional arrays in Ruby so you can represent tables and grids. This is for anyone searching for "Ruby 2D array how to use".

Putting arrays inside an array gives you a grid with rows and columns. This is called a two-dimensional array, and you access it with two indexes, as in grid[row][column].

The sample code builds a 2x3 grid and reads the value at row 2, column 3 with grid[1][2]. Notice how grid.each { |row| puts row.join(",") } prints each row readably.

A common stumbling block for beginners is swapping the row and column indexes. Writing grid[column][row] reaches a completely different position. Deciding up front which index is which prevents the confusion.

Knowing that the data is actually stored in a single line in memory deepens your understanding. It is the basic idea behind game boards with coordinates and spreadsheet-like data.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Ruby directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)