【发布时间】:2010-09-23 04:10:30
【问题描述】:
当它实际上没有被定义时,它得到值nil 只是因为它被“触摸”了:
$ irb
ruby-1.9.2-p0 > foo = true if !defined? foo
=> nil
ruby-1.9.2-p0 > foo
=> nil
ruby-1.9.2-p0 > if !defined? bar
ruby-1.9.2-p0 ?> bar = true
ruby-1.9.2-p0 ?> end
=> true
ruby-1.9.2-p0 > bar
=> true
所以if ... end 按预期工作,但foo = true if ... 没有。
【问题讨论】:
-
这是因为 foo 是在您调用
defined?时定义的。我不明白这是出乎意料的 -
NullUserException: 所以它不先做'if'检查?很有趣。
标签: ruby