I have got the following Ruby hash:
hash = {
0 => "
===
@@@
@ @
@ @
@ @
@@@
===",
1 => "
=
@
@
@
@
@
="}
I would like to print out some of the values of the hash in one line in the console. To that effect, I have created an array with the elements I would like printed out:
test = [hash[0], hash[1]]
or
test1 = [hash[0], hash[0]]
In case I want to print test1
to the console, the result should be the following:
======
@@@@@@
@ @@ @
@ @@ @
@ @@ @
@@@@@@
======
In case I want to print `test2 to the console, the result should be:
====
@@@@
@ @@
@ @@
@ @@
@@@@
====
However, when I use puts
or print
, the result is always that one is printed after another and not in the same line.
You need to create a two-dimensional structure first to be able to get the wanted result.
I suggest the following steps:
Deconstruct the values in your hash
atomic = hash.values.map{ |e| e.split("\n")}
This will give you
[["",
"===",
"@@@",
"@ @",
"@ @",
"@ @",
"@@@",
"==="
], [
"",
"=",
"@",
"@",
"@",
"@",
"@",
"="
]]
Use the new data structure to build the output you need
first case:
test1 = atomic[0].zip(atomic[0]).map(&:join)
puts test1
=>
======
@@@@@@
@ @@ @
@ @@ @
@ @@ @
@@@@@@
======
second case:
test2 = atomic[0].zip(atomic[1]).map(&:join)
=>
====
@@@@
@ @@
@ @@
@ @@
@@@@
====
I hope you find that helpful.