parentheses vs braces

32 views
Skip to first unread message

Dave Castellano

unread,
Apr 25, 2012, 2:21:11 PM4/25/12
to rubyonra...@googlegroups.com
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")

--
Posted via http://www.ruby-forum.com/.

David Hill

unread,
Apr 25, 2012, 2:26:58 PM4/25/12
to rubyonra...@googlegroups.com
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. 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Dave Castellano

unread,
Apr 25, 2012, 3:15:36 PM4/25/12
to rubyonra...@googlegroups.com
So why the = sign in the first example?

Colin Law

unread,
Apr 25, 2012, 3:40:07 PM4/25/12
to rubyonra...@googlegroups.com
On 25 April 2012 20:15, Dave Castellano <li...@ruby-forum.com> wrote:
> 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

Linus Pettersson

unread,
Apr 25, 2012, 4:03:15 PM4/25/12
to rubyonra...@googlegroups.com
u.update_attributes(...)  is a method and you give it a hash as parameter
u.attributes = ... just assigns the attributes variable with a hash.

Leandro Facchinetti

unread,
Apr 27, 2012, 10:33:02 AM4/27/12
to rubyonra...@googlegroups.com
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.

Robert Walker

unread,
Apr 30, 2012, 6:21:37 PM4/30/12
to rubyonra...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages