From: Mike Gold Date: 2009-01-26T08:35:29+09:00 Subject: Re: unalias a method? Todd Burch wrote: > > I'm assuming that alias_method is what I need to be using to temporarily > "redirect" all my Foo.hello calls to do something different for a > certain period of time, and finally I want to revert to the original > behavior of Foo.hello. > In that case, maybe push/pop would make things easier. module MethodStack class << self def included(includer) stack = Hash.new define_method(:push_method) { |method_name, &block| unless stack.has_key?(method_name) stack[method_name] = [instance_method(method_name)] end stack[method_name].push(block) define_method(method_name, &block) } define_method(:pop_method) { |method_name| stack[method_name].pop define_method(method_name, stack[method_name].last) } end end end class Foo class << self include MethodStack end def hello p "original hello" end end obj = Foo.new obj.hello # => "original hello" class Foo push_method(:hello) { p "replacement hello" } end obj.hello # => "replacement hello" Foo.pop_method(:hello) obj.hello # => "original hello" -- Posted via http://www.ruby-forum.com/.