Forum: Ruby Need help on Object#respond_to?

249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-08-18 16:47
I just have seen from the documentation of Object#respond_to?
(http://www.ruby-doc.org/core-2.0/Object.html#metho...)
the below two lines :

- If the method is not implemented, as Process.fork on Windows,
File.lchmod on GNU/Linux, etc., false is returned.

- If the method is not defined, respond_to_missing? method is called and
the result is returned.

Are these 2 lines have different meaning? If so can you explain what
they mean by defining or implementing?
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-08-19 08:42
It means that from the outside of Object#respond_to?, "not defined" and
"not implemented" look the same.

From the inside, it differentiates between the two because a method
could be "defined" in Ruby, but not "implemented" on the current
platform.

There are specific exceptions if you need to specify the difference,
although for the purposes of most users, you shouldn't need to go into
that level of detail.

http://www.ruby-doc.org/core-2.0/NotImplementedError.html

http://www.ruby-doc.org/core-2.0/NoMethodError.html
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-08-19 09:46
Joel Pearson wrote in post #1119074:
> It means that from the outside of Object#respond_to?, "not defined" and
> "not implemented" look the same.
>
> From the inside, it differentiates between the two because a method
> could be "defined" in Ruby, but not "implemented" on the current
> platform.

> http://www.ruby-doc.org/core-2.0/NotImplementedError.html
> http://www.ruby-doc.org/core-2.0/NoMethodError.html


Ok.. Let me tell you why I asked this question and why the documentation
a bit unclear to me.

Suppose I called "hello".respond_to?(:foo),then I am quite sure I would
get `false` back. Now my question is - Is `respond_to_missing?` method
is being called or not,in this scenario ?
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-08-19 10:42
Love U Ruby wrote in post #1119077:
> Suppose I called "hello".respond_to?(:foo),then I am quite sure I would
> get `false` back. Now my question is - Is `respond_to_missing?` method
> is being called or not,in this scenario ?

What's not clear about this?

"If the method is not defined, respond_to_missing? method is called"
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-08-19 11:04
Joel Pearson wrote in post #1119080:
> Love U Ruby wrote in post #1119077:
>> Suppose I called "hello".respond_to?(:foo),then I am quite sure I would
>> get `false` back. Now my question is - Is `respond_to_missing?` method
>> is being called or not,in this scenario ?
>
> What's not clear about this?
>
> "If the method is not defined, respond_to_missing? method is called"

Yes there is a point of doubt. So you mean the `false` is returned by
`respond_to_missing?` method in my example. Right?
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-08-19 12:23
Yes, as far as I can see from the C code, all that the
respond_to_missing? method does is return false. The real work is done
inside respond_to? and the methods it calls.
D9ebdcb66f1583378e6f72155db507e2?d=identicon&s=25 Hans Mackowiak (hanmac)
on 2013-08-20 08:03
#respond_to_missing? is needed when you want to use #method_missing with
#method



class A
def method_missing(m,*)
p m
end
end

A.new.method(:abc) #=> Error

class A
def respond_to_missing?(m,*)
true
end
end

A.new.method(:abc) #=> Ok
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-08-21 21:34
Hans Mackowiak wrote in post #1119151:
> #respond_to_missing? is needed when you want to use #method_missing with
> #method


very interesting..when I tried your code,got a method object,instead of
`true` - Why? Could you please tell me how these below 2 methods work
actually?

class A
  def method_missing(m,*)
    p m
  end
  def respond_to_missing?(m,*)
    true
  end
end

A.new.method(:abc) # => #<Method: A#abc>

Now I write the below one..still working with #method_missing method.

class B
  def respond_to_missing?(m,*)
    true
  end
end

B.new.method(:abc) # => #<Method: B#abc>
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-08-31 14:12
Hans Mackowiak wrote in post #1119151:
> #respond_to_missing? is needed when you want to use #method_missing with
> #method

From some efforts I put,I can tell you that #respond_to_missing? is no
way connected to the method #method_missing,rather it works with
#respond_to? .

class Foo
  def respond_to_missing?(meth,include_private = false)
    "I am being called by #respond_to?"
  end
  def baz
    "I do exist"
  end
end

foo = Foo.new
foo.respond_to?(:baz)
# => true
foo.respond_to?(:bar)
# => true # probably `respond_to_missing?` returns the truth value of
the object,rather the object.

See again:-

class Foo
  def respond_to_missing?(meth,include_private = false)
    nil
  end
  def baz
    "I do exist"
  end
end

foo = Foo.new
foo.respond_to?(:baz) # => true
foo.respond_to?(:bar) # => false

**method_missing**

class Foo
  def method_missing(meth)
    "#{meth} not found"
  end
end

foo = Foo.new
foo.bar
# => "bar not found"
foo.method(:bar)
# ~> -:8:in `method': undefined method `bar' for class `Foo' (NameError)
# ~>  from -:8:in `<main>'

Now to handle this error I think I need to override the method
#respond_to_missing? .

class Foo
  def respond_to_missing?(meth,include_private = false)
    p "checking if I am being called or not"
  end
  def baz
    "I do exist"
  end
  def method_missing(meth)
    "#{meth} not found"
  end
end

foo = Foo.new
foo.bar
# => "bar not found"
foo.method(:bar)
# >> "checking if I am being called or not"

conclusions :-

From this examples I can think of 2 situations,where I should override
the method `#method_missing` and `respond_to_missing?`.

Any other cases do you guys wanted me to point,when I must need to think
of  `method_missing` or `respond_to_missing?` to override ? Obviously
overriding both of them cool way to handle any errors.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.