# Hash or not a Hash?!

**URL:** <https://www.ruby-forum.com/t/hash-or-not-a-hash/191699>\
**Category:** Rails\
**Created:** [July 16, 2010, 1:39pm UTC](https://www.ruby-forum.com/t/hash-or-not-a-hash/191699 "2010-07-16T13:39:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 16, 2010, 1:39pm UTC](https://www.ruby-forum.com/t/hash-or-not-a-hash/191699/1 "2010-07-16T13:39:30Z")

</div>

In Rails, we can see the following for example (They are separate here):

:x=\>“new\_x”

:y=\>{:x=\>X.new}

From Ruby, I know that a hash has to be between { }, and the key is on  
the left of =\> and the value to the right.

But, in the above examples we are seeing =\> everywhere! What is =\> ? Is  
it like = (assignment operator)?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 16, 2010, 2:03pm UTC](https://www.ruby-forum.com/t/hash-or-not-a-hash/191699/2 "2010-07-16T14:03:12Z")

</div>

Abder-Rahman A. wrote:

> In Rails, we can see the following for example (They are separate here):
> 
> :x=\>“new\_x”
> 
> :y=\>{:x=\>X.new}
> 
> From Ruby, I know that a hash has to be between { }, and the key is on  
> the left of =\> and the value to the right.
> 
> But, in the above examples we are seeing =\> everywhere! What is =\> ? Is  
> it like = (assignment operator)?
> 
> Thanks.

This symbol (=\>) is used to assign the value to the key.  
Hashes use this notation extensively.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 16, 2010, 3:03pm UTC](https://www.ruby-forum.com/t/hash-or-not-a-hash/191699/3 "2010-07-16T15:03:27Z")

</div>

> :x=\>“new\_x”

Here x is the key and “new\_x” is the value.

> :y=\>{:x=\>X.new}

Here y is the key and another hash, {:x =\> X.new}, is the value. And  
within this hash again, x is the key and X.new is the value.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 16, 2010, 7:18pm UTC](https://www.ruby-forum.com/t/hash-or-not-a-hash/191699/4 "2010-07-16T19:18:57Z")

</div>

Abder-Rahman A. wrote:

> In Rails, we can see the following for example (They are separate here):
> 
> :x=\>“new\_x”
> 
> :y=\>{:x=\>X.new}
> 
> From Ruby, I know that a hash has to be between { }

But you are wrong. The braces can be omitted when there is no  
ambiguity.

Any time you see =\>, a Hash is involved. That syntax has no other use.

> , and the key is on  
> the left of =\> and the value to the right.
> 
> But, in the above examples we are seeing =\> everywhere! What is =\> ? Is  
> it like = (assignment operator)?
> 
> Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 16, 2010, 7:25pm UTC](https://www.ruby-forum.com/t/hash-or-not-a-hash/191699/5 "2010-07-16T19:25:10Z")

</div>

On Fri, Jul 16, 2010 at 09:02, Ram [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > :x=\>“new\_x”
> 
> Here x is the key and “new\_x” is the value.
> 
> > :y=\>{:x=\>X.new}
> 
> Here y is the key and another hash, {:x =\> X.new}, is the value. And  
> within this hash again, x is the key and X.new is the value.

I think what’s confusing him is that the outer hash isn’t within {}s  
– he doesn’t realize that the {}s aren’t _always_ necessary, that  
there are implicit hashes in some method calls.

-Dave

–  
Specialization is for insects. | Professional: [http://davearonson.com](http://davearonson.com)  
-Robert Anson Heinlein | Programming: [http://codosaur.us](http://codosaur.us)  
-------------------------------+ Leadership: [http://dare2xl.com](http://dare2xl.com)  
Have Pun, Will Babble! -me | Et Cetera: [http://davearonson.net](http://davearonson.net)

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 16, 2010, 10:02pm UTC](https://www.ruby-forum.com/t/hash-or-not-a-hash/191699/6 "2010-07-16T22:02:10Z")

</div>

Robert W. wrote:

> I also wanted to point out that there is a new “optional” syntax for  
> hash key/value pairs introduced in Ruby 1.9:
> 
> { ❌ y } same as { :x =\> y } same as ❌ y same as :x =\> y

[CORRECTION] Sorry, the above syntax is not quite right.

New 1.9 syntax should be:

y = 10  
{ x: y }  
=\> {:x=\>10}

Sorry for any confusion.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 17, 2010, 6:40pm UTC](https://www.ruby-forum.com/t/hash-or-not-a-hash/191699/7 "2010-07-17T18:40:03Z")

</div>

Thanks all for your replies.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 16, 2010, 9:03pm UTC](https://www.ruby-forum.com/t/hash-or-not-a-hash/191699/8 "2010-07-16T21:03:37Z")

</div>

Dave A. wrote:

> On Fri, Jul 16, 2010 at 09:02, Ram [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½\> :x=\>“new\_x”
> > 
> > Here x is the key and “new\_x” is the value.
> > 
> > > :y=\>{:x=\>X.new}
> > 
> > Here y is the key and another hash, {:x =\> X.new}, is the value. And  
> > within this hash again, x is the key and X.new is the value.

More accurately the symbol :y is the key (splitting hairs)…

> I think what’s confusing him is that the outer hash isn’t within {}s  
> – he doesn’t realize that the {}s aren’t _always_ necessary, that  
> there are implicit hashes in some method calls.

Technically speaking Marnen’s reply (“The braces can be omitted when  
there is no  
ambiguity.”) is more accurate.

I also wanted to point out that there is a new “optional” syntax for  
hash key/value pairs introduced in Ruby 1.9:

{ ❌ y } same as { :x =\> y } same as ❌ y same as :x =\> y

So what’s the business about ambiguity?

my\_obj.my\_method(:x =\> y, :a =\> b)

Was the intent of the above to call my\_method with a single hash with  
two keys and two values, or to pass two single key hashes to my\_method?

Sometimes the {} are required to remove ambiguity:

my\_obj.my\_method({ :x =\> y, :a =\> b })

> > 1 hash argument containing 2 key/value pairs

my\_obj.my\_method({ :x =\> y }, { :a =\> b })

> > 2 hash arguments each with a single key/value pair

my\_obj.my\_method([{ :x =\> y }, { :a =\> b }])

> > 1 array argument containing two hash elements, each having a single key/value pair

my\_obj.my\_method([{ :x =\> y, :a =\> b }])

> > 1 array argument containing one hash with two key/value pairs
