Test for instance method existence?

J

Jari Williamsson

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
 
T

Todd Benson

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
 
N

Nobuyoshi Nakada

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
 
L

Lloyd Zusman

Nobuyoshi Nakada said:
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.
 
R

Robert Klemme

2008/2/13 said:
Nobuyoshi Nakada said:
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
 
T

Tom M

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?
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top