From: Lloyd Zusman Date: 2008-02-13T22:13:24+09:00 Subject: Re: Test for instance method existence? Nobuyoshi Nakada ruby-lang.org> writes: > > Hi, > > At Wed, 13 Feb 2008 19:58:56 +0900, > Todd Benson wrote in [ruby-talk:290915]: > > > How can I test if an instance method is defined for a class, without > > > having to create an instance of that class? > > > > > > [ ... ] > > > > I don't know if it's the best way, but there is #instance_methods > > > [ ... ] > > It's nice to find all matching methods, but inefficient when > you know the exact name what you want to know. > > $ ruby -e 'p String.method_defined?(:length)' > true I have a related question. How do I test whether a certain function is defined within TOPLEVEL_BINDING? For example: def foobar puts "toplevel foobar" end class X def self.foobar puts "class X foobar" end def test_foobar # print "exists" if "foobar" is defined within # the top level; print "doesn't exist" if "foobar" # is not defined within the top level. end end x = X.new x.test_foobar What do I put within the test_foobar method to tell me whether the top-level function "foobar" is defined? Also, I don't want that method to get confused by the existence of "self.foobar" within class X. Thanks in advance. -- Lloyd Zusman ljz@asfast.com God bless you.