From: Rick DeNatale Date: 2008-05-15T09:43:18+09:00 Subject: Re: Override methods from a module On Wed, May 14, 2008 at 7:24 PM, Dave Rothlisberger wrote: >> Please let me know if this makes sense/how this could be improved. >> >> module Stuff >> def self.included mod >> @@mixer_class = mod >> end >> >> def initialize >> super >> @@mixer_class.class_eval do >> alias_method :old_setup, :setup >> alias_method :old_teardown, :teardown >> >> define_method(:setup) do >> old_setup >> puts "new setup" >> end >> >> define_method(:teardown) do >> old_teardown >> puts "new teardown" >> end >> end >> end >> end > > A slightly nicer way to write the above: > > module Stuff > def self.included(mod) > mod.class_eval do > alias_method_chain :setup, :new_feature > end > end > > def setup_with_new_feature > setup_without_new_feature > puts 'new setup' > end > > def teardown_with_new_feature > teardown_without_new_feature > puts 'new teardown' > end > end alias_method_chain, nice as it is, is part of activesupport (i.e. part of Rails) and is not standard Ruby. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/