我有一门课:
class TestClass
def method1
end
def method2
end
def method3
end
end
我怎样才能得到这个类中的方法列表(method1
method2
method3
)?
我有一门课:
class TestClass
def method1
end
def method2
end
def method3
end
end
我怎样才能得到这个类中的方法列表(method1
method2
method3
)?
你实际上想要TestClass.instance_methods
,除非你对TestClass
本身的功能感兴趣.
class TestClass
def method1
end
def method2
end
def method3
end
end
TestClass.methods.grep(/method1/) # => []
TestClass.instance_methods.grep(/method1/) # => ["method1"]
TestClass.methods.grep(/new/) # => ["new"]
或者,您可以对对象调用methods
(而不是instance_methods
):
test_object = TestClass.new
test_object.methods.grep(/method1/) # => ["method1"]