Today you are going to learn how to deal with grids in Ruby using the Ruby transpose method.

Imagine that you have a perfect grid, let’s say a 3×3 square, in the form of a multi-dimensional array.

And you want to take the rows & convert them into columns.

Why would you want to do that?

One use is for the classic game: tic-tac-toe.

You store your board as a grid. And to find a winning move you have to check the **rows**, **columns** & **diagonals**.

The problem is that if you are storing your grid as an array you only get direct access to the rows.

By “direct access” I mean that you can go over your array (with `each`

, `map`

, etc.) without having to use more array indexing than necessary.

Let’s see an example!

**Here’s a grid**:

1 2 3 4 5 |
grid = [ [1,2,3], [4,5,6], [7,8,9] ] |

**Here’s a visual I made for you**:

You could get the columns by referencing the indexes.

**For example, the first column would be**:

1 2 3 |
[grid[0][0], grid[1][0], grid[2][0]] # [1, 4, 7] |

**But the first row is just this**:

1 2 3 |
grid[0] # [1, 2, 3] |

How can we make working with columns as easy as working with rows?

The easy way to do this is by using the Array#transpose method.

**Example**:

1 |
columns = grid.transpose |

Yes, that’s all you have to do!

Now you can get the first column like this:

1 2 3 |
columns[0] # [1, 4, 7] |

As you can see knowing a lot of methods can save you a lot of work ðŸ™‚

I’m not going to explain the whole thing. I just want to show you how this method can apply to a real project.

To win a game of tic-tac-toe you need to fill a row, a column or a diagonal.

**Here’s the code for checking rows**:

1 2 3 |
def check_rows @board.each { |row| return row.first if all_equal?(row) } end |

**And here’s is the code for columns**:

1 2 3 |
def check_columns @board.transpose.each { |row| return row.first if all_equal?(row) } end |

Notice how the only difference is the `transpose`

method!

Here’s the `all_equal?`

method:

1 2 3 4 5 |
def all_equal?(row) return if row.first == nil row.each_cons(2).all? { |x,y| x == y } end |

You can learn more about that each_cons method by reading this post on Enumerable methods.

You have learned about the transpose method.

Given a perfect grid, transpose allows you to transform the rows into columns for easy access.

If you enjoyed this post you may want to consider buying a copy of my book Ruby Deep Dive.