面试题答案
一键面试class MyClass
def my_method
end
def another_method
end
end
method_names = MyClass.instance_methods(false)
puts method_names.inspect
在上述代码中:
MyClass.instance_methods(false)
用于获取MyClass
类自身定义的实例方法名称,不包含从祖先类继承的方法。false
参数表示不包含继承的方法,如果省略该参数或为true
,则会包含继承的方法。- 最后使用
puts
输出这些方法名称的数组。