operator =>

G

Gian Holland

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 :eek:rders, :through *=>* :line_items
#...
 
S

Stefano Crocco

Alle marted=EC 29 maggio 2007, Gian Holland ha scritto:
Hi I have a beginner question

What does the =3D> operator do?

here is the example code from the book

class Product < ActiveRecord::Base
has_many :eek:rders, :through *=3D>* :line_items
#...

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

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

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

has_many( :eek:rders, {:through =3D> :line_items} )

In other words, you're passing two arguments to the method has_many: the fi=
rst=20
is the Symbol :eek:rders; the second is a Hash with one key :)through) and one=
=20
value :)line_items)

Stefano
 
G

Gian Holland

Thanks so much

Alle marted=EC 29 maggio 2007, Gian Holland ha scritto:

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

h =3D {:a =3D> 1, :b=3D>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 met= hod,
ruby allows you to omit the braces, so your call to has many means:

has_many( :eek:rders, {:through =3D> :line_items} )

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

Stefano
 
P

Pierre-Alexandre Meyer

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

Nobuyoshi Nakada

Hi,

At Thu, 31 May 2007 17:24:42 +0900,
Pierre-Alexandre Meyer wrote in [ruby-talk:253715]:
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}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top