From: me@... Date: 2007-04-11T17:05:07+09:00 Subject: Re: What happens to instance variables in modules when they are extended into classes On Apr 10, 9:33 pm, Brian Candler wrote: > On Tue, Apr 10, 2007 at 11:25:06PM +0900, m...@mikehogan.net wrote: > > Why is it that an instance method of the module gets mixed into the > > class, but instance variables of the module do not get mixed in? > > > Whats the best strategy for writing instance methods in a module that > > depend on some state in the mixee? > > > Thanks very much, > > Mike. > > > And now here is the code snippet: > > > module M > > @module_instance_variable = 'miv' > > > def module_instance_method > > @module_instance_variable > > end > > end > > > class C > > extend M > > end > > Presumably you understand why the following code prints 'nil': > > class C > @foo = "hello" > > def bar > puts @foo > end > end > C.new.bar > > So I'm not sure what you're trying to achieve with your code snippet. Do you > want @module_instance_variable to be an instance variable of module M, or an > instance variable of class C, or an instance variable of instances of class > C? > > What you have at the moment is an instance variable of M, as shown by: > > puts M.module_eval { @module_instance_variable } > > If you want to set up instance variables of class C itself (remembering that > many different C's may each extend M), then one approach is > > module M > def self.extended(obj) > puts "hello!" > obj.instance_eval { @miv = "miv" } > end > end > > class C > extend M > end > > puts C.instance_variables > > HTH, > > Brian. Ok - yes thats twigged it for me. Thanks very much Brian. Mike