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/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. On Wed, Apr 25, 2012 at 1:21 PM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> 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/. > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
So why the = sign in the first example? -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 April 2012 20:15, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
u.update_attributes(...) is a method and you give it a hash as parameter u.attributes = ... just assigns the attributes variable with a hash. Den onsdagen den 25:e april 2012 kl. 21:15:36 UTC+2 skrev Ruby-Forum.com User:> > So why the = sign in the first example? > > -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/rwqHHSgWfwsJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> > 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/-Ck4qI5DOi0J. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.