parentheses vs braces

Working thru a Rails tutorial...

Can anyone tell me why the first example uses braces and the second example uses parentheses? Is it because 1st example is a hash literal, second is not?

u = User.find(2) u.attributes = {:name =>"Fred", :color => "green"} u.save

u = User.find(2) u.update_attributes(:name =>"Fred", :color => "green")

Someone can correct me if I'm wrong, but the second example actually is a hash. You wrote:

u.update_attributes(:name =>"Fred", :color => "green")

The arguments you passed in are a hash. It's the same thing as writing:

u.update_attributes({:name =>"Fred", :color => "green"})

Ruby (I think...or is it Rails?) gives you the freedom that if the 'last' parameter passed to a method is a hash, you can omit the { } characters.

So why the = sign in the first example?

Do you mean in u.attributes = {:name =>"Fred", :color => "green"}

Because that is assigning the hash to the variable u.attributes.

Please quote the previous message when replying in order to avoid the necessity of looking back at previous emails to work out what the message is referring to.

Colin

u.update_attributes(…) is a method and you give it a hash as parameter

u.attributes = … just assigns the attributes variable with a hash.

Ruby (I think…or is it Rails?) gives you the freedom that if the ‘last’ parameter passed to a method is a hash, you can omit the { } characters.

It’s Ruby. If the last argument in a function call is a hash, you can omit the braces.

Leandro Facchinetti wrote in post #1058817:

It's Ruby. If the last argument in a function call is a hash, you can omit the braces.

I think technically it has to do with ambiguity. It doesn't really have anything do with it being the last argument, but rather omitting the braces anywhere else in the argument list would cause ambiguity. Although, that's really just semantics.