From: Rail Shafigulin Date: 2010-12-21T11:18:37+09:00 Subject: Re: creating an object 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? -- Posted via http://www.ruby-forum.com/.