From: Avdi Grimm Date: 2008-02-29T02:10:15+09:00 Subject: Re: Proposed Solutions - Was [ Re: Monkeypatching is Destroying Ruby ] On Thu, Feb 28, 2008 at 9:39 AM, hemant wrote: > My point is, I love open classes, but it shouldn't hurt to have little > bit of safety precaution built into the language, does it? In the mean time, a quick hack: http://pastie.caboo.se/paste/158825 Example: >> load 'redefine.rb' => true >> ?> class Module >> include Redefine >> end => Module >> ?> class Monkey >> def ook_ook >> puts "ook ook!" >> load 'redefine.rb' => true >> ?> class Module >> include Redefine >> end => Module >> ?> class Monkey >> def ook_ook >> puts "ook ook!" >> end >> end => nil >> ?> class Monkey >> redefine_method(:ook_ook) { "please, may I have another banana?" } >> end Redefine::MethodRedefinitionError: Method 'ook_ook' redefined from (irb):14 >> ?> Monkey.new.ook_ook ook ook! => nil >> ?> Redefine.allow_collisions= :warn => :warn >> ?> class Monkey >> redefine_method(:ook_ook) { "please, may I have another banana?" } >> end Method 'ook_ook' redefined at (irb):22:in `irb_binding' => # >> ?> Monkey.new.ook_ook => "please, may I have another banana?" >> ?> Redefine.allow_collisions= true => true >> ?> class Monkey >> redefine_method(:ook_ook) { "Look out, I'm in a poo-flinging mood" } >> end => # >> ?> Monkey.new.ook_ook => "Look out, I'm in a poo-flinging mood" >> -- Avdi