# Hash questions

**URL:** <https://rubytalk.org/t/hash-questions/52763>\
**Category:** ruby-talk\
**Created:** [9 April 2009 14:18 UTC](https://rubytalk.org/t/hash-questions/52763 "2009-04-09T14:18:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hunt\_Jon](https://avatars.discourse-cdn.com/v4/letter/h/ea5d25/32.png) [@Hunt\_Jon](https://rubytalk.org/u/Hunt_Jon)\
**Post date:** [9 April 2009 14:18 UTC](https://rubytalk.org/t/hash-questions/52763/1 "2009-04-09T14:18:09Z")

</div>

When I run:

p {"foo" =\> 1, "bar" =\> 2, "baz" =\> 3 }

I get an error.

But when I run:

x = {"foo" =\> 1, "bar" =\> 2, "baz" =\> 3 }  
p x

There's no error.

What's the difference between the two?

John

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [9 April 2009 14:28 UTC](https://rubytalk.org/t/hash-questions/52763/2 "2009-04-09T14:28:48Z")

</div>

p( {"foo" =\> 1, "bar" =\> 2, "baz" =\> 3} )

You need parentheses in the first case, I believe, so the that the  
parser knows you are not trying to send a block to the method #p.

Todd

> **···**
>
> On Thu, Apr 9, 2009 at 9:18 AM, Hunt Jon \<jona.hunt777@gmail.com\> wrote:
> 
> > When I run:
> > 
> > p {"foo" =\> 1, "bar" =\> 2, "baz" =\> 3 }
> > 
> > I get an error.
> > 
> > But when I run:
> > 
> > x = {"foo" =\> 1, "bar" =\> 2, "baz" =\> 3 }  
> > p x
> > 
> > There's no error.
> > 
> > What's the difference between the two?
> > 
> > John

---

<div class="post-metadata">

**Author:** ![Adam\_Gardner](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@Adam\_Gardner](https://rubytalk.org/u/Adam_Gardner)\
**Post date:** [9 April 2009 14:33 UTC](https://rubytalk.org/t/hash-questions/52763/3 "2009-04-09T14:33:18Z")

</div>

Hunt Jon wrote:

> When I run:
> 
> p {"foo" =\> 1, "bar" =\> 2, "baz" =\> 3 }
> 
> I get an error.
> 
> But when I run:
> 
> x = {"foo" =\> 1, "bar" =\> 2, "baz" =\> 3 }  
> p x
> 
> There's no error.
> 
> What's the difference between the two?
> 
> John

From what I can tell, ruby is interpreting the beginning of your hash as  
the beginning of a block, instead;  
p({"foo" =\> 1, "bar" =\> 2, "baz" =\> 3 }) works as intended.

This is seems like a bug to me, although whether it's a bug with the  
parser (finding a block where it should be looking for a hash literal),  
a bug with p (asking for a block when it doesn't use one) or the  
documentation (failing to mention that p accepts a block), I'm not sure.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Adam\_Gardner](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@Adam\_Gardner](https://rubytalk.org/u/Adam_Gardner)\
**Post date:** [9 April 2009 14:35 UTC](https://rubytalk.org/t/hash-questions/52763/4 "2009-04-09T14:35:31Z")

</div>

> From what I can tell, ruby is interpreting the beginning of your hash as  
> the beginning of a block, instead;  
> p({"foo" =\> 1, "bar" =\> 2, "baz" =\> 3 }) works as intended.
> 
> This is seems like a bug to me, although whether it's a bug with the  
> parser (finding a block where it should be looking for a hash literal),  
> a bug with p (asking for a block when it doesn't use one) or the  
> documentation (failing to mention that p accepts a block), I'm not sure.

Actually, on a bit more inspection, both puts and print work the same  
way, and I imagine there are other methods that do this as well. This  
just seems like the parser failing to correctly interpret the ambiguity  
of a {.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [9 April 2009 14:44 UTC](https://rubytalk.org/t/hash-questions/52763/5 "2009-04-09T14:44:53Z")

</div>

No, I'd say that the parser is correctly parsing an initial '{' after a  
method invocation as a block argument to the invocation.

There's no need to mention that p, or any other method can be given a block  
since ANY ruby method can be given (and by default) ignore a block. It's  
just part of the language that you need to learn, like the need to use  
self.x = value to invoke an attribute writer method to disambiguate between  
x= as a method and x as a local variable.

> **···**
>
> On Thu, Apr 9, 2009 at 10:35 AM, Adam Gardner \<adam.oddfellow@gmail.com\>wrote:
> 
> > \> From what I can tell, ruby is interpreting the beginning of your hash as  
> > \> the beginning of a block, instead;  
> > \> p({"foo" =\> 1, "bar" =\> 2, "baz" =\> 3 }) works as intended.  
> > \>  
> > \> This is seems like a bug to me, although whether it's a bug with the  
> > \> parser (finding a block where it should be looking for a hash literal),  
> > \> a bug with p (asking for a block when it doesn't use one) or the  
> > \> documentation (failing to mention that p accepts a block), I'm not sure.
> > 
> > Actually, on a bit more inspection, both puts and print work the same  
> > way, and I imagine there are other methods that do this as well. This  
> > just seems like the parser failing to correctly interpret the ambiguity  
> > of a {.
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [9 April 2009 14:50 UTC](https://rubytalk.org/t/hash-questions/52763/6 "2009-04-09T14:50:04Z")

</div>

Here's an alternative

p( "foo" =\> 1, "bar" =\> 2, "baz" =\> 3 )

And no, the observed behavior is not a bug as Rick pointed out.

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 09.04.2009 16:44, Rick DeNatale wrote:
> 
> > [Note: parts of this message were removed to make it a legal post.]
> > 
> > On Thu, Apr 9, 2009 at 10:35 AM, Adam Gardner \<adam.oddfellow@gmail.com\>wrote:
> > 
> > > > From what I can tell, ruby is interpreting the beginning of your hash as  
> > > > the beginning of a block, instead;  
> > > > p({"foo" =\> 1, "bar" =\> 2, "baz" =\> 3 }) works as intended.

---

<div class="post-metadata">

**Author:** ![Phlip1](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Phlip1](https://rubytalk.org/u/Phlip1)\
**Post date:** [9 April 2009 14:55 UTC](https://rubytalk.org/t/hash-questions/52763/7 "2009-04-09T14:55:03Z")

</div>

Robert Klemme wrote:

> p( "foo" =\> 1, "bar" =\> 2, "baz" =\> 3 )
> 
> And no, the observed behavior is not a bug as Rick pointed out.

The freedom to skip () parens nearly everywhere comes at a price. You gotta think of the space following a method name as a tiny little operator meaning "introduce arguments here like a ( would have".
