On Sun, 13 Jan 2002, Yukihiro Matsumoto wrote: > > In message "respond_to? and public/private" > on 02/01/11, Johan Holmberg <holmberg / iar.se> writes: > > |I think the default value of that parameter is a bit unintuitive. > |Wouldn't it be better if the default was depending on the call > |context ?, ie. that > | > | ......respond_to? :foo...... > | > |told if > | > | ......foo(arguments)...... > | > |would succeed in finding a callable function/method. > > Do you mean > > respond_to? :foo > > reports including private methods, and > > obj.respond_to? :foo > > reports only public methods? What about "protected"? > I was rather "naive" and looked for some way to decide if a method invocation would succeed in finding a callable method (taking into account public/protected/private distinctions in the current scope). Something like ... foo_callable = true begin a.foo() # of just "foo()" if I was interested in that rescue NameError foo_callable = false end ... but without actually calling the method/function. (I know the example isn't prefect: it's only valid for methods with no arguments. But I hope you get what I was looking for.) > How about using "defined?" without complicating "respond_to?" > behavior? > > defined? print # => "method" > defined? self.print # => nil > Oops, I missed that ... "defined?" seems to do almost exactly what I want. But I found *one* minor difference from what I actually expected. defined? reports "method" for a protected method, even though it is *not* callable. Example: class A def protected_fun "protected_fun" end protected :protected_fun end a = A.new p defined? a.protected_fun # prints "method" p a.protected_fun # gives exception NameError Is this behaviour intentional, or is it a bug ? /Johan Holmberg