Forum: Ruby how to test for existence of a method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-04-22 07:23
(Received via mailing list)
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
3ca98e75ecd686304f9a4aae2b25ac09?d=identicon&s=25 Chris Kottom (Guest)
on 2009-04-22 07:26
(Received via mailing list)
Object#respond_to? doesn't do it for you?
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-04-22 08:02
(Received via mailing list)
Thanks. Does just fine - now that I know about it. Handy!

t.

Chris Kottom wrote:
>> exists? I'd like to do something better than just catch an exception
>
>


--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-22 12:43
(Received via mailing list)
2009/4/22 Tom Cloyd <tomcloyd@comcast.net>:
> Thanks. Does just fine - now that I know about it. Handy!
>
> t.
>
> Chris Kottom 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
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-04-22 17:25
(Received via mailing list)
On Wed, Apr 22, 2009 at 12:43 PM, Robert Klemme
<shortcutter@googlemail.com> 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.
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-04-22 19:35
(Received via mailing list)
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 Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-04-22 19:45
(Received via mailing list)
On Wed, Apr 22, 2009 at 7:34 PM, Tom Cloyd <tomcloyd@comcast.net> 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
1bc63d01bd3fcccc36fb030a62039352?d=identicon&s=25 David Masover (Guest)
on 2009-04-22 23:02
(Received via mailing list)
On Wednesday 22 April 2009 05:43:06 Robert Klemme 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).
This topic is locked and can not be replied to.