[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Test for instance method existence?

Jari Williamsson

2/13/2008 10:47:00 AM

How can I test if an instance method is defined for a class, without
having to create an instance of that class?

Simple example: Test if 'length' defined for the 'String' class?


Best regards,

Jari Williamsson

5 Answers

Todd Benson

2/13/2008 10:59:00 AM

0

On Feb 13, 2008 4:47 AM, Jari Williamsson
<jari.williamsson@mailbox.swipnet.se> wrote:
> How can I test if an instance method is defined for a class, without
> having to create an instance of that class?
>
> Simple example: Test if 'length' defined for the 'String' class?
>
>
> Best regards,
>
> Jari Williamsson

I don't know if it's the best way, but there is #instance_methods

irb(main):008:0> Fixnum.instance_methods.grep /ins/
=>["inspect", "instance_variable_defined?", "instance_variables",
"instance_variable_get", "instance_of?", "instance_eval",
"instance_variable_set"]

Todd

Nobuyoshi Nakada

2/13/2008 12:42:00 PM

0

Hi,

At Wed, 13 Feb 2008 19:58:56 +0900,
Todd Benson wrote in [ruby-talk:290915]:
> > How can I test if an instance method is defined for a class, without
> > having to create an instance of that class?
> >
> > Simple example: Test if 'length' defined for the 'String' class?
> >
> >
> > Best regards,
> >
> > Jari Williamsson
>
> I don't know if it's the best way, but there is #instance_methods
>
> irb(main):008:0> Fixnum.instance_methods.grep /ins/
> =>["inspect", "instance_variable_defined?", "instance_variables",
> "instance_variable_get", "instance_of?", "instance_eval",
> "instance_variable_set"]

It's nice to find all matching methods, but inefficient when
you know the exact name what you want to know.

$ ruby -e 'p String.method_defined?(:length)'
true

--
Nobu Nakada

Lloyd Zusman

2/13/2008 1:13:00 PM

0

Nobuyoshi Nakada <nobu <at> ruby-lang.org> writes:

>
> Hi,
>
> At Wed, 13 Feb 2008 19:58:56 +0900,
> Todd Benson wrote in [ruby-talk:290915]:
> > > How can I test if an instance method is defined for a class, without
> > > having to create an instance of that class?
> > >
> > > [ ... ]
> >
> > I don't know if it's the best way, but there is #instance_methods
> >
> [ ... ]
>
> It's nice to find all matching methods, but inefficient when
> you know the exact name what you want to know.
>
> $ ruby -e 'p String.method_defined?(:length)'
> true

I have a related question. How do I test whether a certain function is
defined within TOPLEVEL_BINDING? For example:

def foobar
puts "toplevel foobar"
end

class X
def self.foobar
puts "class X foobar"
end
def test_foobar
# print "exists" if "foobar" is defined within
# the top level; print "doesn't exist" if "foobar"
# is not defined within the top level.
end
end

x = X.new
x.test_foobar

What do I put within the test_foobar method to tell me whether the top-level
function "foobar" is defined? Also, I don't want that method to get confused
by the existence of "self.foobar" within class X.

Thanks in advance.

--
Lloyd Zusman
ljz@asfast.com
God bless you.



Robert Klemme

2/13/2008 1:30:00 PM

0

2008/2/13, Lloyd Zusman <ljz@asfast.com>:
> Nobuyoshi Nakada <nobu <at> ruby-lang.org> writes:
>
> >
> > Hi,
> >
> > At Wed, 13 Feb 2008 19:58:56 +0900,
> > Todd Benson wrote in [ruby-talk:290915]:
> > > > How can I test if an instance method is defined for a class, without
> > > > having to create an instance of that class?
> > > >
> > > > [ ... ]
> > >
> > > I don't know if it's the best way, but there is #instance_methods
> > >
> > [ ... ]
> >
> > It's nice to find all matching methods, but inefficient when
> > you know the exact name what you want to know.
> >
> > $ ruby -e 'p String.method_defined?(:length)'
> > true
>
> I have a related question. How do I test whether a certain function is
> defined within TOPLEVEL_BINDING? For example:
>
> def foobar
> puts "toplevel foobar"
> end
>
> class X
> def self.foobar
> puts "class X foobar"
> end
> def test_foobar
> # print "exists" if "foobar" is defined within
> # the top level; print "doesn't exist" if "foobar"
> # is not defined within the top level.

eval("self", TOPLEVEL_BINDING).method(:foobar) rescue false

> end
> end
>
> x = X.new
> x.test_foobar
>
> What do I put within the test_foobar method to tell me whether the top-level
> function "foobar" is defined? Also, I don't want that method to get confused
> by the existence of "self.foobar" within class X.
>
> Thanks in advance.

YWC

robert

--
use.inject do |as, often| as.you_can - without end

Tom M

2/13/2008 3:12:00 PM

0

On Feb 13, 5:47 am, Jari Williamsson
<jari.williams...@mailbox.swipnet.se> wrote:
> How can I test if an instance method is defined for a class, without
> having to create an instance of that class?
>
> Simple example: Test if 'length' defined for the 'String' class?
>
> Best regards,
>
> Jari Williamsson

Do you want all the instance methods, or just the public ones?