[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to test for existence of a method

Tom Cloyd

4/22/2009 5:22:00 AM

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



7 Answers

Chris Kottom

4/22/2009 5:26:00 AM

0

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

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

On Wed, Apr 22, 2009 at 7:22 AM, Tom Cloyd <tomcloyd@comcast.net> wrote:

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

Tom Cloyd

4/22/2009 6:02:00 AM

0

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

t.

Chris Kottom wrote:
> Object#respond_to? doesn't do it for you?
>
> On Wed, Apr 22, 2009 at 7:22 AM, Tom Cloyd <tomcloyd@comcast.net> wrote:
>
>
>> 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
>>
>>
>>
>>
>>
>
>


--

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


Robert Klemme

4/22/2009 10:43:00 AM

0

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Jesús Gabriel y Galán

4/22/2009 3:24:00 PM

0

On Wed, Apr 22, 2009 at 12:43 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
>> 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>

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.

Tom Cloyd

4/22/2009 5:35:00 PM

0

Jesús Gabriel y Galán wrote:
> On Wed, Apr 22, 2009 at 12:43 PM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
>
>>> 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>
>>
>
> 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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Robert Dober

4/22/2009 5:44:00 PM

0

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=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

David Masover

4/22/2009 9:02:00 PM

0

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