From: "Sandor Szücs" Date: 2013-01-06T20:41:12+09:00 Subject: Re: respond_to_missing? On 1/5/13 12:42 PM, Intransition wrote: > Thank you Jan and Sandor. That is basically what I thought, but I had > recently become confused b/c of this: > > https://bugs.ruby-lang.org/issues/5759 > > If you read toward the bottom of this thread, there is a case that seems > contradictory. I still don't get it. > The difference of respond_to? and respond_to_missing? is that you can get a Method object using Kernel#method(sym) if a method will be dynamically implemented using method_missing. Here again the example: 13 class Bar1 14 def respond_to_missing? *args 15 true 16 end 17 def method_missing(*args, &blk) 18 p args 19 end 20 end 21 22 class Baz1 23 def respond_to? *args 24 true 25 end 26 27 def method_missing(*args, &blk) 28 p args 29 end 30 end # get the method: irb:0> Bar1.new.method :foo => # # do not get the method: irb:0> Baz1.new.method :foo NameError: undefined method `foo' for class `Baz1' from (irb):17:in `method' from (irb):17 from /.../.rvm/rubies/ruby-1.9.3-p362/bin/irb:16:in `
' In your example you define: "#to_ary() is not created dynamically nor defined static" class TransBaz1 def method_missing(s) s end def respond_to_missing?(s, x) return false if s == :to_ary true end end irb:0> TransBaz1.new.method :to_ary NameError: undefined method `to_ary' for class `TransBaz1' from (irb):45:in `method' from (irb):45 from /.../.rvm/rubies/ruby-1.9.3-p362/bin/irb:16:in `
' irb:0> TransBaz1.new.method :foo => # You can not get Method object :to_ary using Kernel#method, but you can get :foo. -- All the best, Sandor Szücs