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.
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?
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.