Ruby &&= edge case
Posted
by Alan O'Donnell
on Stack Overflow
See other posts from Stack Overflow
or by Alan O'Donnell
Published on 2010-06-06T02:57:42Z
Indexed on
2010/06/06
3:02 UTC
Read the original article
Hit count: 313
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!
© Stack Overflow or respective owner