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 W.
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 W.
On Feb 13, 2008 4:47 AM, Jari W.
[email protected] 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 W.
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
Hi,
At Wed, 13 Feb 2008 19:58:56 +0900,
Todd B. wrote in [ruby-talk:290915]:
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
Nobuyoshi N. <nobu ruby-lang.org> writes:
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.
On Feb 13, 5:47 am, Jari W.
[email protected] 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 W.
Do you want all the instance methods, or just the public ones?
2008/2/13, Lloyd Z. [email protected]:
[ … ]
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
Thanks in advance.
YWC
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs