Ruby &&= edge case
- by Alan O'Donnell
Bit of an edge case, but any idea why &&= would behave this way? I'm using 1.9.2.
obj = Object.new
obj.instance_eval {@bar &&= @bar} # => nil, expected
obj.instance_variables # => [], so obj has no @bar instance variable
obj.instance_eval {@bar = @bar && @bar} # ostensibly the same as @bar &&= @bar
obj.instance_variables # => [:@bar] # why would this version initialize @bar?
For comparison, ||= initializes the instance variable to nil, as I'd expect:
obj = Object.new
obj.instance_eval {@foo ||= @foo}
obj.instance_variables # => [:@foo], where @foo is set to nil
Thanks!