Ruby 1.9.2 Object.respond_to? :hello && Object.hello gives error, why?

767 views Asked by At

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

4

There are 4 answers

2
John On BEST ANSWER

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
0
Robert Horvick On

I believe you are running into an evaluation order problem. Consider the following:

Object.respond_to? :hello && true 
3
Tim Knight On

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.

0
mu is too short On

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.