[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

operator =>

Gian Holland

5/29/2007 11:33:00 AM

Hi I have a beginner question

What does the => operator do?

here is the example code from the book

class Product < ActiveRecord::Base
has_many :orders, :through *=>* :line_items
#...


4 Answers

Stefano Crocco

5/29/2007 11:44:00 AM

0

Alle martedì 29 maggio 2007, Gian Holland ha scritto:
> Hi I have a beginner question
>
> What does the => operator do?
>
> here is the example code from the book
>
> class Product < ActiveRecord::Base
> has_many :orders, :through *=>* :line_items
> #...

=> is not an operator, its simply the syntax used to create hashes:

h = {:a => 1, :b=>2}

creates an hash with keys :a and :b, corresponding to values 1 and 2
respectively. When you need to pass an hash as the last argument of a method,
ruby allows you to omit the braces, so your call to has many means:

has_many( :orders, {:through => :line_items} )

In other words, you're passing two arguments to the method has_many: the first
is the Symbol :orders; the second is a Hash with one key (:through) and one
value (:line_items)

Stefano



Gian Holland

5/30/2007 5:57:00 PM

0

Thanks so much

On 5/29/07, Stefano Crocco <stefano.crocco@alice.it> wrote:
> Alle martedì 29 maggio 2007, Gian Holland ha scritto:
> > Hi I have a beginner question
> >
> > What does the => operator do?
> >
> > here is the example code from the book
> >
> > class Product < ActiveRecord::Base
> > has_many :orders, :through *=>* :line_items
> > #...
>
> => is not an operator, its simply the syntax used to create hashes:
>
> h = {:a => 1, :b=>2}
>
> creates an hash with keys :a and :b, corresponding to values 1 and 2
> respectively. When you need to pass an hash as the last argument of a method,
> ruby allows you to omit the braces, so your call to has many means:
>
> has_many( :orders, {:through => :line_items} )
>
> In other words, you're passing two arguments to the method has_many: the first
> is the Symbol :orders; the second is a Hash with one key (:through) and one
> value (:line_items)
>
> Stefano
>
>
>
>

Pierre-Alexandre Meyer

5/31/2007 8:25:00 AM

0

On Thu, May 31, 2007 at 02:57:19AM +0900, Gian Holland wrote :
> >=> is not an operator, its simply the syntax used to create hashes:
> >
> >h = {:a => 1, :b=>2}

Looking at the trunk, my understanding is that it will be replaced by :
in a next release? Correct?

--
,========================.
| Pierre-Alexandre Meyer |
| email : pam@mouraf.org |
`========================'

Nobuyoshi Nakada

6/1/2007 1:37:00 AM

0

Hi,

At Thu, 31 May 2007 17:24:42 +0900,
Pierre-Alexandre Meyer wrote in [ruby-talk:253715]:
> > >h = {:a => 1, :b=>2}
>
> Looking at the trunk, my understanding is that it will be replaced by :
> in a next release? Correct?

It's a syntax sugar but traditional syntax is still valid. The
above example is equivalent to:

h = {a: 1, b: 2}

--
Nobu Nakada