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 H. 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 Thu, May 31, 2007 at 02:57:19AM +0900, Gian H. 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?

Hi,

At Thu, 31 May 2007 17:24:42 +0900,
Pierre-Alexandre M. 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}

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