From: Ammar Ali Date: 2010-01-18T19:56:52+09:00 Subject: Re: what does this mean? 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? > > Kind regards > > robert > > 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?) I should have shown some of the other elegant ways to achieve this in ruby, like Robert did. Or at least pointed out that what I posted is not the way to do things. I guess I assumed that no one would actually type what I did once they figured out what the one liner does. Thanks, ammar