Hash or not a Hash?!

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.

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.

: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.

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.

On Fri, Jul 16, 2010 at 09:02, Ram [email protected] 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
-Robert Anson Heinlein | Programming: http://codosaur.us
-------------------------------+ Leadership: http://dare2xl.com
Have Pun, Will Babble! -me | Et Cetera: http://davearonson.net

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:

{ :x: y } same as { :x => y } same as :x: 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.

Thanks all for your replies.

Dave A. wrote:

On Fri, Jul 16, 2010 at 09:02, Ram [email protected] 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:

{ :x: y } same as { :x => y } same as :x: 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs