How to test for existence of a method


#1

I’m a little stumped on this one. I looked at several ideas, and none
panned out.

I have a string, which may name a method in a module required by my
program. How can I test programmatically to see if the method actually
exists? I’d like to do something better than just catch an exception
generated when I call a non-existent method.

Is there a way to do this?

Tom


#2

Object#respond_to? doesn’t do it for you?


#3

Thanks. Does just fine - now that I know about it. Handy!

t.

Chris K. wrote:

exists? I’d like to do something better than just catch an exception

Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

#4

2009/4/22 Tom C. removed_email_address@domain.invalid:

Thanks. Does just fine - now that I know about it. Handy!

t.

Chris K. wrote:

Object#respond_to? doesn’t do it for you?

This is unsafe:

irb(main):001:0> o = Object.new
=> #Object:0x10171bb8
irb(main):002:0> def o.method_missing(s,*a,&b) s == :foo ? “yes” : super
end
=> nil
irb(main):003:0> o.respond_to? :foo
=> false
irb(main):004:0> o.respond_to? “foo”
=> false
irb(main):005:0> o.foo
=> “yes”
irb(main):006:0>

The best approach is to actually invoke the method and deal with
exceptions IMHO (-> duck typing).

Kind regards

robert


#5

Jesús Gabriel y Galán wrote:

=> #Object:0x10171bb8

Just a question: isn’t it good practice to override respond_to? when
you use method missing to handle things, so that it “tells the truth”?
Or is it something people don’t usually do?

Jesus.

How would you do this?

t.

Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

#6

On Wednesday 22 April 2009 05:43:06 Robert K. wrote:

The best approach is to actually invoke the method and deal with
exceptions IMHO (-> duck typing).

I think that depends on the situation. If respond_to? is known to work,
it’s
still a lot more duck-friendly than kind_of?, and is probably the
easiest
choice, unless it really would be exceptional for it not to have that
method.

But yes, it’s true – overriding respond_to? gets you partway there, but
you
never know how an object is going to respond to a method until you call
it.
After all, from your perspective, there’s really no difference between

class Foo
end

and

class Foo
def foo
raise NoMethodError …
end
end

Once you consider that, it becomes obvious that there may be a situation
where
respond_to? either can’t be reliable, or would be expensive (for
example, if
you’re doing something like DRb).


#7

On Wed, Apr 22, 2009 at 7:34 PM, Tom C. removed_email_address@domain.invalid wrote:

How would you do this?
Well by mirroring the logic of method_missing
two cases were this can be done simply

def method_missing *args, &blk
delegation.send *args, &blk
end

def respond_to? name
delegation.respond_to? name
end


def method_missing *args, &blk
fetch( args.first.to_sym ){ super } # déjà vu :wink:
end

def respond_to? name
has_key name.to_sym
end

==========================

Jesus’ question was shall one do this, I believe that more often than
not one shall indeed do this.
Robert’s warning remains very valuable though, because I believe that
very often one does
not go through the trouble.

Cheers
Robert


#8

On Wed, Apr 22, 2009 at 12:43 PM, Robert K.
removed_email_address@domain.invalid wrote:

irb(main):003:0> o.respond_to? :foo
=> false
irb(main):004:0> o.respond_to? “foo”
=> false
irb(main):005:0> o.foo
=> “yes”
irb(main):006:0>

Just a question: isn’t it good practice to override respond_to? when
you use method missing to handle things, so that it “tells the truth”?
Or is it something people don’t usually do?

Jesus.