From: John Lam Date: 2006-05-13T02:44:08+09:00 Subject: Re: Inheriting class-based state ------=_Part_30209_9714442.1147455845569 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Thanks, Joel - this is exactly what I needed to fix some ugliness in RubyCLR. I was trying to get class variables to work earlier, and actually ran acros= s the problem that you mentioned in your (*). I just didn't know it at the time :) Cheers, -John http://www.iunknown.com On 5/12/06, Joel VanderWerf wrote: > > An alternative: > > class A > class << self > attr_accessor :__my_name > > def my_name; A.__my_name; end > def my_name=3D(n); A.__my_name =3D n; end > end > end > > class B < A > end > > B.my_name =3D "fred" > > p A.my_name # =3D=3D> "fred" > > Or, if you don't want __my_name polluting the namespace, you could use > this: > > class A > class << self > def my_name; A.instance_variable_get(:@my_name); end > def my_name=3D(n); A.instance_variable_set(:@my_name, n); end > end > end > > class B < A > end > > B.my_name =3D "fred" > > p A.my_name > > The key difference between these two examples and your original attempt > is that, here, the "A" reference binds _statically_ to the class A, > whereas, in the original code, the "@my_name" reference binds > _dynamically_ to the instance on which my_name is called (which might > turn out to be A, or B or ...). > > HTH. > > ------=_Part_30209_9714442.1147455845569--