7
是否可以從單個實例中刪除方法?僅從實例中刪除方法
class Foo
def a_method
"a method was invoked"
end
end
f1 = Foo.new
puts f1.a_method # => a method was invoked
我可以從已創建的對象的類的刪除a_method本:
class Foo
remove_method(:a_method)
end
如果我從同一個對象調用a_method:
puts f1.a_method # => undefined method
如果我創建另一個object:
f2 = Foo.new
puts f2.a_method # => undefined method
如何才能從特定的單個對象中刪除方法?
你能不能給更多的信息在你的用例中呢? – rdubya 2014-11-23 22:32:27
重新定義該實例的方法以拋出異常可能更容易。 – 2014-11-23 22:33:20