how to test for existence of a method

T

Tom Cloyd

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
 
C

Chris Kottom

[Note: parts of this message were removed to make it a legal post.]

Object#respond_to? doesn't do it for you?
 
T

Tom Cloyd

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

t.

Chris said:
Object#respond_to? doesn't do it for you?


--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< (e-mail address removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
R

Robert Klemme

2009/4/22 Tom Cloyd said:
Thanks. Does just fine - now that I know about it. Handy!

t.

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
 
J

Jesús Gabriel y Galán

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>

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.
 
T

Tom Cloyd

Jesús Gabriel y Galán said:
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 Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< (e-mail address removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
R

Robert Dober

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=E9j=E0 vu ;)
end

def respond_to? name
has_key name.to_sym
end

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D

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
 
D

David Masover

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top