From: Robert Klemme Date: 2008-02-13T06:54:55+09:00 Subject: Re: How to add a instance variable through a mixin? On 12.02.2008 20:54, MrBanabas@googlemail.com wrote: >> If you mix the module in after some instances of MyClass have already >> been created, do you want it to dynamically create the instance >> variables in those instances? >> >> If you mix the module in before some instances of MyClass have been >> created, do you want it to run code each time a new instance is >> created? > > I would be interested in both cases, but for my particularly case it s > the second one. You can do module Foo attr_accessor :var def initialize(*a,&b) @var = 10 end end And then irb(main):015:0> class Bar irb(main):016:1> include Foo irb(main):017:1> end => Bar irb(main):018:0> Bar.new.var => 10 Or irb(main):026:0> class Oink irb(main):027:1> include Foo irb(main):028:1> def initialize irb(main):029:2> super irb(main):030:2> @x = 10 irb(main):031:2> end irb(main):032:1> end => nil irb(main):033:0> Oink.new.var => 10 Kind regards robert