From: Robert Klemme Date: 2004-07-02T17:22:48+09:00 Subject: Re: mixin question "Ara.T.Howard" schrieb im Newsbeitrag news:Pine.LNX.4.60.0407011009461.25990@harp.ngdc.noaa.gov... > > > i have this > > jib:~ > cat a.rb > module M > def bar > @bar = 42 > super > end > end > class Foo > attr :bar > def initialize > @bar = 'bar' > end > end > > foo = Foo.new > p foo.bar > > class Foo > include M # of course this acts as super - it does not override > end > > p foo.bar > > > jib:~ > ruby a.rb > "bar" > "bar" > > and i want it to print "bar", "42". i'm looking for a way to overide all of a > classes methods via a module (or some other trick) without needing to extend > individual objects and without simply opening up the class and redefining > everything. in otherwords a simple way to override a bunch of bundled methods > at once. it seems like this must be possible but my brain isn't firing on all > fours today... Does this help? module M def self.included(cl) cl.instance_eval do instance_methods.each do |meth| define_method(meth) do |*args| p args end end end end def bar @bar = 42 super end end class Foo attr :bar def initialize @bar = 'bar' end end foo = Foo.new p foo.bar class Foo include M # of course this acts as super - it does not override end p foo.bar Of course the method definition has to be made a bit smarter. Regards robert