From: Francis Cianfrocca Date: 2006-05-23T06:28:45+09:00 Subject: Re: initializing instance variables in a module ------=_Part_210087_12442696.1148333322383 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Looks promising but what if you have to assign to the variable? This code throws an exception: module Mixin def myvar @myvar ||=3D 100 @myvar end def inc_myvar myvar +=3D 1 end end class Klass include Mixin end Klass.new.inc_myvar On 5/22/06, Simon Kr=F6ger wrote: > > Francis Cianfrocca schrieb: > > You still have to call it every time you use the variable. > > > > Do you realy need to use the variable directly? > My scripts tend to have very little use of '@'. > > module Foo > def bar(key) > @myvar ||=3D Hash.new {|k,v| k[v] =3D []} > @myvar[key] > end > end > > class Test > include Foo > end > > t =3D Test.new > t.bar('baz') << 'eeek' > p t.bar('baz') > > > I didn't followed the whole thread so please ignore me > if this was suggested before. > > cheers > > Simon > > ------=_Part_210087_12442696.1148333322383--