How do I print a multi-dimensional array in ruby? How do I print a multi-dimensional array in ruby? ruby ruby

How do I print a multi-dimensional array in ruby?


If you're just looking for debugging output that is easy to read, "p" is useful (it calls inspect() on the array)

p x[[1, 2, 3], [4, 5, 6]]


Either:

p x

-or-

require 'pp'. . .        pp x


If you want to take your multi-dimensional array and present it as a visual representation of a two dimensional graph, this works nicely:

x.each do |r|  puts r.each { |p| p }.join(" ")end

Then you end with something like this in your terminal:

  1 2 3  4 5 6  7 8 9