Search code examples
rubystringtransformationtext-formatting

How can I transform array of arrays into one string in Ruby


I have an array of arrays looking like this:

arr = [[0,0,0,0,0], [0,0,1,0,0], [0,1,0,1,0], [0,0,1,0,0], [0,0,0,0,0]]

and I want to make it look like this:

 00000
 00100
 01010
 00100
 00000

I tried like this:

arr.each {|a| p a.join(',').gsub(',','')}

but it outputs it like this:

 00000
 00100
 01010
 00100
 00000

whith quotes ' " " ' in the begining and the end of each row. I want it to be one single piece that starts with a quote then the rows and in the end - quote. But not quoting every single row.


Solution

  • join without an argument:

    arr.each{|el| puts el.join}