From: Robert Klemme Date: 2010-01-18T20:51:32+09:00 Subject: Re: what does this mean? 2010/1/18 Ammar Ali : > Robert Klemme wrote: >> >> 2010/1/18 Ammar Ali : >> >>> >>> Ruby Newbee wrote: >>> >>>> >>>> For this statement: >>>> >>>> @speaks_english ||= @name.size > 6 >>>> >>>> does it mean something like below? >>>> >>>> if @name.size > 6 >>>>   @speaks_english ||= @name.size >>>> end >>>> >>>> >>>> Even if I guess that correctly, I'm still surprised why ruby can write >>>> like that way. >>>> >>> >>> More like: >>> >>> if @speaks_english.nil? or @speaks_english == false >>>  if @name > 6 >>>  @speaks_english = true >>>  else >>>  @speaks_english = false >>>  end >>> end >>> >> >> I'd shorten that to >> >> unless @speaks_english >>  @speaks_english = @name.size > 6 >> end >> >> "@name.size > 6" is a boolean expression and it does return "true" or >> "false" already. >> >> Note that in this case this would probably a better solution because >> it does not reevaluate if @name.size <= 6: >> >> @speaks_english = @name.size > 6 if @speaks_english.nil? > > I intentionally expanded || to nil? or false tests, and "unrolled" the > assignment to @name.size > 6 to illustrate the boolean result. I hoped the > painfully procedural rendering would get the point across quickly and maybe > cause some head scratching (where did all that come from?) Well, the question is how many levels deep one "unrolls" the code. I tried to stick with one level but it's as valid of course to go deeper. I just felt that it might obscure the answer to the original question a bit. > I guess I assumed that no one would actually type what I did > once they figured out what the one liner does. Absolutely agree. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/