creating an object

R

Rail Shafigulin

i'm somewhat new to rails and ruby.

whenever i create a new object in ruby i use

o = ClassName.new

but recently i bumped into the following code (in rails)

rating = Rating.new:)rating => params[:rating])

i can't understand what it means. i looked at the class definition for
Rating and didn't see any attributes or methods named rating. i also
didn't see the constructor in there (i.e. initialize method) i would
really appreciate if someone offer an explanation of what is happening
and if there are more ways of creating an object.

any help is appreciated
 
S

Steve Klabnik

[Note: parts of this message were removed to make it a legal post.]

The reason you don't see an initialize method is because Rails models
inherit from ActiveRecord::Base, which sets that up for you.
 
J

John Morrice

rating = Rating.new:)rating => params[:rating])
i can't understand what it means.

It's just a regular call to Rating.new, but it's using the special
syntax for sending a hash as an argument.

:rating is a symbol, used as the key in the hash. It refers to
params[:rating].

Try playing with this in irb

# Tested with ruby 1.9.2
# Lets see what food is in the fridge!
def peek fridge
puts "fridge was a #{fridge.class}"
puts "The cheese was: #{fridge[:cheese]}"
puts "The vegetable was: #{fridge[:vegetable]}"
end

# This is closer to the code in your problem
peek:)cheese => :cheddar, :vegetable => :leeks)

# But you can do it with hands free syntax too!
peek :cheese => :brie, :vegetable => :broccoli

Read about this in the pickaxe
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html#UE

(or as more uncouth elements may say, RTFM)

Johnny

P.S. Whoever wrote that piece of code seems to really like the word
rating.
 
R

Rail Shafigulin

Johnny M. wrote in post #969685:
rating = Rating.new:)rating => params[:rating])

i can't understand what it means.

It's just a regular call to Rating.new, but it's using the special
syntax for sending a hash as an argument.

:rating is a symbol, used as the key in the hash. It refers to
params[:rating].

Try playing with this in irb

# Tested with ruby 1.9.2
# Lets see what food is in the fridge!
def peek fridge
puts "fridge was a #{fridge.class}"
puts "The cheese was: #{fridge[:cheese]}"
puts "The vegetable was: #{fridge[:vegetable]}"
end

# This is closer to the code in your problem
peek:)cheese => :cheddar, :vegetable => :leeks)

# But you can do it with hands free syntax too!
peek :cheese => :brie, :vegetable => :broccoli

Read about this in the pickaxe
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html#UE

(or as more uncouth elements may say, RTFM)

Johnny

P.S. Whoever wrote that piece of code seems to really like the word
rating.

i understood that it was a hash, but i couldn't figure what is going to
happen when we pass a hash to the new method. i couldn't find the
initialize method in the class definition.

i tried the following code in irb but i received an error

class SomeClass
end

s = SomeClass.new:)rating => params[:rating])

does it mean that i simply initialize is defined somewhere else since i
received an error?
 
J

Jesús Gabriel y Galán

Johnny M. wrote in post #969685:
rating =3D Rating.new:)rating =3D> params[:rating])

i can't understand what it means.

It's just a regular call to Rating.new, but it's using the special
syntax for sending a hash as an argument.

:rating is a symbol, used as the key in the hash. =A0It refers to
params[:rating].

Try playing with this in irb

# Tested with ruby 1.9.2
# Lets see what food is in the fridge!
def peek fridge
=A0 =A0puts "fridge was a #{fridge.class}"
=A0 =A0puts "The cheese was: #{fridge[:cheese]}"
=A0 =A0puts "The vegetable was: #{fridge[:vegetable]}"
end

# This is closer to the code in your problem
peek:)cheese =3D> :cheddar, :vegetable =3D> :leeks)

# But you can do it with hands free syntax too!
peek :cheese =3D> :brie, :vegetable =3D> :broccoli

Read about this in the pickaxe
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html#UE

(or as more uncouth elements may say, RTFM)

Johnny

P.S. Whoever wrote that piece of code seems to really like the word
rating.

i understood that it was a hash, but i couldn't figure what is going to
happen when we pass a hash to the new method. i couldn't find the
initialize method in the class definition.

i tried the following code in irb but i received an error

class SomeClass
end

s =3D SomeClass.new:)rating =3D> params[:rating])

does it mean that i simply initialize is defined somewhere else since i
received an error?

Yes, most probably, your Rating class inherits from
ActiveRecord::Base, which as Steve said sets up a lot of method for
your class, one of them being the initialize method that receives a
hash.

Jesus.
 

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

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,366
Latest member
IanCulpepp

Latest Threads

Top