From: "David A. Black" Date: 2008-04-05T19:13:27+09:00 Subject: Re: The ||= assignment operator Hi -- On Sat, 5 Apr 2008, Avdi Grimm wrote: > On this topic, am I the only one who would really like to see a > dedicated "defaulting operator" in Ruby? Something that actually has > the semantics which a lot of people mistakenly assume for ||=, i.e. > "assign if and only if nil"? I think Perl6 uses the //= operator for > this. This is such a subtle gotcha. I've seen a lot of code that was > potentially buggy because it didn't account for the fact that the > 'false' value would be handled incorrectly. The false value isn't handled incorrectly, though. The test is for boolean falseness, and both nil and false always pass that test. If you need x to be a certain value (nil or any other), you should test for exactly that value. Also, it's not exactly that the lhs of ||= is nil. ||= will allow uninitialized local variables, which are not nil: irb(main):003:0> a ||= 1 => 1 irb(main):004:0> b || b = 1 NameError: undefined local variable or method `b' for main:Object from (irb):4 So we'd be getting into a thing with "uninitialized or nil, but not false" which seems very use-case specific to me. I'd rather let the boolean significance just do its thing. David -- Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS April 14-17 New York City INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin See http://www.rubypal.com for details and updates!