Drew Olson wrote:
Your examples definitely help and may have helped me with another way of
looking at the problem but I'm not sure they'll work as my objects are
already extending parent objects.
- Drew
Sorry about the previous response. Apparently lack of sleep results in
horrendous grammar. Below is the solution I've figured out for the
problem. It seems to work the way I was hoping for. Please let me know
if this makes sense/how this could be improved.
Thanks,
Drew
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
class Tester
def common
puts "this is in all tests!"
end
end
class PlainTester < Tester
def setup
puts "plain setup"
end
def teardown
puts "plain teardown"
end
end
class ExtraFunTester < Tester
include Stuff
def setup
puts "old setup"
end
def teardown
puts "old teardown"
end
end
Not a general solution:
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
class Tester
def common
puts "this is in all tests!"
end
end
class PlainTester < Tester
def setup
puts "plain setup"
end
def teardown
puts "plain teardown"
end
end
class ExtraFunTester < Tester
include Stuff
def setup
puts "old setup"
end
def teardown
puts "old teardown"
end
end
class FunkedUpTester < Tester
include Stuff
def setup
puts "old setup"
end
def teardown
puts "old teardown"
end
end
p = PlainTester.new
e = ExtraFunTester.new
f = FunkedUpTester.new
puts "p.setup"
p.setup
p.teardown
puts "e.setup"
e.setup
e.teardown
puts "f.setup"
f.setup
f.teardown
RubyMate r8136 running Ruby r1.8.6
(/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby)
untitled
p.setup
plain setup
plain teardown
e.setup
old setup
old teardown
f.setup
SystemStackError: stack level too deep
method setup in untitled document at line 13
method old_setup in untitled document at line 13
method setup in untitled document at line 13
method old_setup in untitled document at line 13
method setup in untitled document at line 13
method old_setup in untitled document at line 13
method setup in untitled document at line 13
···
On Mon, Oct 15, 2007 at 7:52 PM, Drew Olson <olsonas@gmail.com> wrote:
,
.
.
method old_setup in untitled document at line 13
method setup in untitled document at line 13
at top level in untitled document at line 78
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/