Operator =>

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

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

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

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

···

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}

--
,========================.

Pierre-Alexandre Meyer |
email : pam@mouraf.org |

`========================'

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