我能找到的最接近的是
In Ruby,how do I check if method “foo=()” is defined?,但只有当方法是公共的时,它才有效,即使在类块内.
我想要的是:
class Foo private def bar "bar" end magic_private_method_defined_test_method :bar #=> true end
我尝试过的:
class Foo private def bar "bar" end respond_to? :bar #=> false #this actually calls respond_to on the class,and so respond_to :superclass gives true defined? :bar #=> nil instance_methods.include?(:bar) #=> false methods.include?(:bar) #=> false method_defined?(:bar) #=> false def bar "redefined!" end # redefining doesn't cause an error or anything public def bar "redefined publicly!" end #causes no error,behaves no differently whether or not #bar had been defined prevIoUsly end