有点边缘情况,但任何想法为什么&& =会这样做?我正在使用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?
Run Code Online (Sandbox Code Playgroud)
为了比较,|| =将实例变量初始化为nil,正如我所期望的那样:
obj = Object.new
obj.instance_eval {@foo ||= @foo}
obj.instance_variables # => [:@foo], where @foo is set to nil
Run Code Online (Sandbox Code Playgroud)
谢谢!
ruby ×1