In Ruby why won't `foo = true unless defined?(foo)` make the assignment?

后端 未结 7 928
悲哀的现实
悲哀的现实 2020-12-03 14:48

What\'s going on here? What is the subtle difference between the two forms of \"unless\"?

> irb(main):001:0> foo = true unless defined?(foo)
=> nil         


        
7条回答
  •  自闭症患者
    2020-12-03 15:04

    irb(main)> foo = true unless defined?(Integer)
    => nil 
    irb(main)> foo = true unless defined?(thisIsUndefined)
    => true
    

    Your first block is returning nil because the way it's written leaves 2 options:

    • foo is not defined --> assign true
    • foo is defined --> do nothing

    Here, foo must be defined when the line is evaluated. Thus, nothing happens and nil is returned.

    irb(main)> unless defined?(Integer) ; fooo = false ; end
    => nil
    irb(main)> unless defined?(thisIsUndefined) ; fooo = false ; end
    => false 
    

    Your second block operates the same way your first one does. If fooo is not defined, the block is entered and fooo is set to false. The result of the last line of the block is the return value of the block, thus the false you are seeing. If fooo does exist, then the block is skipped over and nothing happens, therefore there is nothing to return, therefore the nil.

    Based on your code, I would say that foo was defined when this code was run and fooo was not (test code shown was generated in Ruby 1.8.6). If you did not define either of these before running this code, then you may have something called foo that is defined by default (do defined?(foo) by itself to check). Try using a different name and see if you get the same results.

    Edit:

    irb(main)> defined?(bar)
    => nil
    irb(main)> bar = true unless defined?(bar)
    => nil
    irb(main)> defined?(bar)
    => "local-variable"
    

    Apparently, defined?() is returning true since it has already seen bar (at the beginning of the line), even though you are still in the process of defining it.

提交回复
热议问题