📜  ruby 是方法定义的 - Ruby 代码示例

📅  最后修改于: 2022-03-11 15:04:47.735000             🧑  作者: Mango

代码示例1
# respond_to?(symbol, include_all=false) works well for testing if an object has a method defined
# .try() will not handle methods that have parameters, respond_to? does handle those methods
if my_obj.respond_to?(:my_method)
    my_obj.my_method(arg1, arg2)
end

# You can also try instance_methods(false).include?(symbol) for class objects
my_class_obj.instance_methods(false).include?(:my_method)