# How to test for existence of a method

**URL:** <https://www.ruby-forum.com/t/how-to-test-for-existence-of-a-method/166140>\
**Category:** Ruby\
**Created:** [April 22, 2009, 7:23am UTC](https://www.ruby-forum.com/t/how-to-test-for-existence-of-a-method/166140 "2009-04-22T07:23:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [April 22, 2009, 7:23am UTC](https://www.ruby-forum.com/t/how-to-test-for-existence-of-a-method/166140/1 "2009-04-22T07:23:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [April 22, 2009, 7:26am UTC](https://www.ruby-forum.com/t/how-to-test-for-existence-of-a-method/166140/2 "2009-04-22T07:26:35Z")

</div>

Object#respond\_to? doesn’t do it for you?

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [April 22, 2009, 8:02am UTC](https://www.ruby-forum.com/t/how-to-test-for-existence-of-a-method/166140/3 "2009-04-22T08:02:53Z")

</div>

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

–

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [April 22, 2009, 12:43pm UTC](https://www.ruby-forum.com/t/how-to-test-for-existence-of-a-method/166140/4 "2009-04-22T12:43:49Z")

</div>

2009/4/22 Tom C. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [April 22, 2009, 7:35pm UTC](https://www.ruby-forum.com/t/how-to-test-for-existence-of-a-method/166140/5 "2009-04-22T19:35:52Z")

</div>

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.

–

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [April 22, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/how-to-test-for-existence-of-a-method/166140/6 "2009-04-22T23:02:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [April 22, 2009, 7:45pm UTC](https://www.ruby-forum.com/t/how-to-test-for-existence-of-a-method/166140/7 "2009-04-22T19:45:18Z")

</div>

On Wed, Apr 22, 2009 at 7:34 PM, Tom C. [removed\_email\_address@domain.invalid](mailto: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 😉  
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

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [April 22, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/how-to-test-for-existence-of-a-method/166140/8 "2009-04-22T17:25:45Z")

</div>

On Wed, Apr 22, 2009 at 12:43 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto: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.
