On Oct 15, 4:27 pm, Drew Olson <olso... / gmail.com> wrote:
> All -
>
> I'd like to create a module that adds some specific functionality to my
> setup and teardown methods within a few of my test cases. I'd like to be
> able to do something to the effect of:
>
> module Stuff
>   method_alias :old_setup, :setup
>   def setup
>     old_setup
>     # new stuff goes here
>   end
> end
>
> class MyTest
>   include Stuff
>   def setup
>     # original setup stuff here
>   end
> end

I've never done this before, but here's a hack that sort of does what
I think you want:

module Foo
  def initialize( *args )
    self.extend Foo::Stuff
  end
  module Stuff
    def bar
      puts "from module"
      super
    end
  end
end

class Bar
  include Foo
  def initialize( name )
    @name = name
    super
  end
  def bar
    puts "from class"
  end
end

goof = Bar.new( 'Goofy' )
goof.bar
#=> from module
#=> from class

__END__

Note that this requires calling super in the initialize of the source
class, which may break other ancestor modules/classes expecting
different arguments to be passed in. Ideally the module would latch
onto and wrap the initialize method itself when included in the class,
but I was too lazy to do that.