23rd Mon
Singleton Methodをundefする
方法を探していたのですが、意外と苦戦したのでメモ。
ruby>>
object = new Object
def object.foo; end
object.singleton_methods #=> [:foo]
(class << object; self end).class_eval{send :remove_method, :foo}
object.singleton_methods #=> []
<<--
これで行けるようです。instance_eval{undef foo}でも削除はできますが、その場合親クラスのメソッドにもアクセス出来なくなってしまうので、
singleton methodだけを削除する場合には、
メタクラスのインスタンスメソッドをremove_methodするのが良いようです。
posted by
genki on Mon 23 Feb 2009 at 21:25 with 0 comments