Ruby expression ‘defined?’
Recently I got bitten by defined?. Here’s the gotcha (the condition always passes and never goes into else…):
Recently I got bitten by defined?. Here’s the gotcha (the condition always passes and never goes into else…):
if defined? MyString && obj.instance_of?(MyString)
# …
else
# …
endIt turned out to be operator precedence (&& has higher precedence).
obj = ‘str’defined? String # => “constant”
defined? String && obj.instance_of?(String) # => “expression”
defined?(String) && obj.instance_of?(String) # => true# MyString is not defined yet
defined? MyString # => nil
defined? MyString && obj.instance_of?(MyString) # => “expression”
defined?(MyString) && obj.instance_of?(MyString) # => falseThe original condition is actually equal to defined?(MyString && obj.instance_of?(MyString)). According to defined?, the expression given to defined? is not executed, which means MyString && obj.instance_of?(MyString) is always an ”expression”.
That’s the price for not memorizing the operator precedence table… However, since defined? has such low precedence, I guess it’s easier to just always add parenthesis.

