# Printing 2 dimensionl hash problem

**URL:** <https://www.ruby-forum.com/t/printing-2-dimensionl-hash-problem/82211>\
**Category:** Ruby\
**Created:** [January 18, 2007, 8:47am UTC](https://www.ruby-forum.com/t/printing-2-dimensionl-hash-problem/82211 "2007-01-18T08:47:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [January 18, 2007, 8:47am UTC](https://www.ruby-forum.com/t/printing-2-dimensionl-hash-problem/82211/1 "2007-01-18T08:47:41Z")

</div>

Hi,

busy with my script, I am trying to print my hash, which is built up as  
follows:

hash:

key = A, value = [1,2,3]  
key = B, value = [4,5,6]

etc.

I try this:

@h.each do |key,value|  
print key, " is ", value.each {|x| print x, " – " }, “\n”

The print is strange, becuase it looks like this now:

1 – 2 – 3 – A is 123  
4 – 5 – 6 – B is 456

I don’t understand why. If I interpreted this correctly, I’ve got the  
following two questions:

1. How come the code block is executed first
2. After that the key and value is printed, however, I don’t see the  
print for the values here (except for the print in de the code block,  
which was executed already).

Krekna

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [January 18, 2007, 9:24am UTC](https://www.ruby-forum.com/t/printing-2-dimensionl-hash-problem/82211/2 "2007-01-18T09:24:43Z")

</div>

From: Krekna M. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] :  
#----------------------------------------------------------

# @h.each do |key,value|

# print key, " is ", value.each {|x| print x, " – " }, “\n”

# 

# The print is strange, becuase it looks like this now:

# 

# 1 – 2 – 3 – A is 123

# 4 – 5 – 6 – B is 456

# 

# 1. How come the code block is executed first

#----------------------------------------------------------

print expects to print a value of the expression (of course). so before  
prints does the writing, it has to get the value first.

#----------------------------------------------------------

# 2. After that the key and value is printed, however, I don’t see the

print for the values here (except for the print in de the code block,  
which was executed already).  
#----------------------------------------------------------

"value.each {…} " is an expression that returns a value, in this case  
the array “value”. that expression however will perform \*first a {|x|  
print x, “—”} for every element x in array “value”.

eg,

C:\temp\rubygems-0.9.1\>cat test.rb

h = {“A” =\> [1,2,3], “B” =\>[4,5,6]}

puts “sample 1”  
h.each do |key,value|  
print key, " is "  
value.each {|x| print x, " – " }  
print “\n”  
end  
puts  
puts “sample 2”  
h.each do |key,value|  
puts “#{key} is #{value.join(’ – ')}”  
end

C:\temp\rubygems-0.9.1\>ruby test.rb  
sample 1  
A is 1 – 2 – 3 –  
B is 4 – 5 – 6 –

sample 2  
A is 1 – 2 – 3  
B is 4 – 5 – 6

hth.  
kind regards -botp

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [January 18, 2007, 11:50am UTC](https://www.ruby-forum.com/t/printing-2-dimensionl-hash-problem/82211/3 "2007-01-18T11:50:22Z")

</div>

2007/1/18, Peña, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> # 1. How come the code block is executed first
> 
> #----------------------------------------------------------
> 
> print expects to print a value of the expression (of course). so before prints does the writing, it has to get the value first.

> #----------------------------------------------------------
> 
> # 2. After that the key and value is printed, however, I don’t see the
> 
> print for the values here (except for the print in de the code block,  
> which was executed already).  
> #----------------------------------------------------------
> 
> "value.each {…} " is an expression that returns a value, in this case the array “value”. that expression however will perform \*first a {|x| print x, “—”} for every element x in array “value”.

Thank you for your explanation!

I see now, that value.each is an argument to the print, which is not  
what I need indeed, because like you said value.each with its code  
block is already printing the values, so that’s why they are printed  
twice.

So, I’ve put the value.each on a separate line like in your example now.  
Thus, the value.each expression is returned first, apparently the  
arguments are parse before the (first) print is executed.

Same as this:  
irb(main):012:0\> print “I”, " am ", "#{print "first “}”  
first I am =\> nil

Krekna

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [January 18, 2007, 10:00am UTC](https://www.ruby-forum.com/t/printing-2-dimensionl-hash-problem/82211/4 "2007-01-18T10:00:50Z")

</div>

> etc.
> 
> I don’t understand why. If I interpreted this correctly, I’ve got the  
> following two questions:
> 
> 1. How come the code block is executed first
> 2. After that the key and value is printed, however, I don’t see the  
> print for the values here (except for the print in de the code block,  
> which was executed already).

🙂 I think what you want to actually do is:

@h.each do |key,value|  
print key, " is ", value.join(’ – '), “\n”  
end

or

@h.each do |key,value|  
puts key, " is ", value.join(’ – ')  
end

To hopefully answer your questions:

You have asked Ruby to print out the return value of value.each{…},  
which is simply value:

[1,2,3] == [1,2,3].each {|n| do\_something\_with\_n\_if\_you\_like(n)}

So that explains the 123 at the end of the line, because [1,2,3].to\_s is  
“123”

Why the “1 – 2 – 3” before everything else though? Well, _before_ Ruby  
can execute that first “print”, it must _first_ evaluate what is being  
printed, just as if you had writen “print 1+2+3”. In calculating that,  
Ruby calls values.each {…}, and this prints out the “1 – 2 – 3” at  
the start of the line.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [January 18, 2007, 11:53am UTC](https://www.ruby-forum.com/t/printing-2-dimensionl-hash-problem/82211/5 "2007-01-18T11:53:22Z")

</div>

Thank you very much, my answer was posted before I read this.  
I think it’s clear to me now.  
That’s the best way, learning by doing and talking about it what  
actually happens when something went wrong.

Thank you too.

Krekna

2007/1/18, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
