This has bitten me a couple times in the last few days. The Ruby “or” (or equals) operator appears to have a bug where, when used in an assignment, the first value is assigned rather than the results of the entire expression.
irb(main):001:0> x = false || true
=> true
irb(main):002:0> x
=> true
irb(main):003:0> z = false or true
=> true
irb(main):004:0> z
=> false
ruby version: ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.10.1]
I would expect z to be true in that last example.
This isn’t a bug. The operator ‘or’ is below the operator ‘=’ in the order of operations, and the operator ‘||’ is above ‘=’. (This is documented in the section ‘Operator Expressions’ here http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html.)
So Ruby parses your first assertion as x = (false || true), and does just what you want. But it parses your second one as (z = false) or true, which means it assigns z to the value of false, and then returns the value true.
If you change your second statement to ‘z = (false or true)’, it should work. Personally, I just avoid that ‘or’ operator in favor of the ‘||’ operator, because I don’t want to deal with stuff like this.