I don''t understand the following syntax, the "||=" and "||nil" @country ||= (@current_user.country || nil) anybody could explain please ? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
The "|| nil" is redundant in this case. Basically, ||= is shorthand for @country = @current_user.country unless @country aka, set the variable if the variable is currently ''nil'' Jason On Fri, Jun 13, 2008 at 2:22 PM, Victor Dias <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I don''t understand the following syntax, the "||=" and "||nil" > > @country ||= (@current_user.country || nil) > > anybody could explain please ? > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
"@current_user.country || nil" would return the @current_user.country value if it is not false and nil if it is. "@country ||= (@current_user.country || nil)" would return the value of @country if it is not nil and the result of the above example if it is. It''s like writing "@country = @country || (@current_user.country || nil) " but nicer. On Jun 13, 7:22 pm, Victor Dias <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I don''t understand the following syntax, the "||=" and "||nil" > > @country ||= (@current_user.country || nil) > > anybody could explain please ? > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
||= is pretty confusing to many people x ||= y expands to x or x = y It is fairly simple unless x in a hash or array, then it gets hairy. Here is David Black''s discussion on the topic http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case For a really drawn out discussion and lots of arguing go here: http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/fe4fbc48e19105cd/bf7f73380e285aff?lnk=gst&q=or+equal#bf7f73380e285aff regards --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Here''s a more verbose translation: if x.nil? or x == false then x = y else x end (Both of those alternatives return the value in x--the first one after the assignment from y.) Usually what''s meant is "give me the value in x, if there is one, but if x is nil, then first assign whatever''s in y to x, and *then* give me the value in x". So this method in one of my models: def self.get_list @@all_organizations ||= self.find(:all, :select => ''id, name'') end Will only hit the database once. After the first time, @@all_organizations won''t be nil and ruby will just return the list previously fetched. -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Ruby Freak Sent: Friday, June 13, 2008 12:31 PM To: Ruby on Rails: Talk Subject: [Rails] Re: could explain syntax "||="? ||= is pretty confusing to many people x ||= y expands to x or x = y It is fairly simple unless x in a hash or array, then it gets hairy. Here is David Black''s discussion on the topic http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case For a really drawn out discussion and lots of arguing go here: http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/fe4fb c48e19105cd/bf7f73380e285aff?lnk=gst&q=or+equal#bf7f73380e285aff regards --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---