From: Stefano Crocco Date: 2008-01-10T19:12:10+09:00 Subject: Re: how to remove a method from an object ? Alle giovedì 10 gennaio 2008, Valerio Schiavoni ha scritto: > hello everyone, > > how can I remove a method from an object, without impacting future > instances of its class ? > > say: > > class Server > def print > puts 'hello' > end > end > > s1 = Server.new > s1.print #this work > > #now remove print from s1 somehow.. > > s1.print #this should hit method_missing > > s2 = Server.new > s2.print # this should work fine as well... > > > > Thanks for any help This should work: class Server def print puts "hello from #{object_id}" end end s1 = Server.new s1.print class << s1 undef_method :print end s2 = Server.new s2.print begin s1.print rescue NoMethodError puts "undefined method 'print' for #{s1.object_id}" end => hello from -605995538 hello from -605995648 undefined method 'print' for -605995538 As you can see from the ids displayed by the print method, after removing the method from the singleton class of s1, only the call to s2.print works; the call to s1.print raises a NoMethodError exception. I hope this helps Stefano