Need help on Object#respond_to?

I just have seen from the documentation of Object#respond_to?
(http://www.ruby-doc.org/core-2.0/Object.html#method-i-respond_to-3F)
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?

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

#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

···

--
Posted via http://www.ruby-forum.com/.

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.

Class: NotImplementedError (Ruby 2.0.0)
Class: NoMethodError (Ruby 2.0.0)

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?(:sum),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 ?

···

--
Posted via http://www.ruby-forum.com/\.

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>

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

--
Posted via http://www.ruby-forum.com/\.

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"

···

--
Posted via http://www.ruby-forum.com/\.

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?

···

--
Posted via http://www.ruby-forum.com/\.