Ruby 1.9.2 Object.respond_to? :hello && Object.hello gives error, why?
While stepping through code today, I noticed something unexpected. This statement:开发者_开发问答
if Object.respond_to? :hello && Object.hello #stuff
gives an undefined method error. But why? Obviously hello is not a valid method of Object, however given the short-circuit evaluation, shouldn't Object.hello be ignored whenever Object.respond_to? :hello is false?
I noticed this while playing with Authlogic, trying to figure out exactly why the UserSession class must define persisted? in Rails 3.
Thanks
The lack of parentheses is leading to a precedence issue:
>> Object.respond_to? :hello && Object.hello
NoMethodError: undefined method `hello' for Object:Class
from (irb):2
from /Users/john/.rvm/rubies/ruby-1.9.2-p136/bin/irb:16:in `<main>'
>> Object.respond_to?(:hello) && Object.hello
=> false
You have a precedence problem, the logical conjunction (&&
) has a higher precedence than the method call so your example is executed like this:
if Object.respond_to?(:hello && Object.hello)
not like this:
if Object.respond_to?(:hello) && Object.hello
which is what you're assuming.
I believe you are running into an evaluation order problem. Consider the following:
Object.respond_to? :hello && true
If you are checking the condition of something with an if statement you are looking for a boolean response. Object.hello doesn't return false because the method doesn't exist. Your check of:
Object.respond_to? :hello
Is checking for that response though. There shouldn't be any reason, in my option for the second Object.hello.
精彩评论