From: Edward Faulkner Date: 2005-10-26T03:40:21+09:00 Subject: Re: having reference problems --s2ZSL+KKDSLx8OML Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Oct 26, 2005 at 03:02:03AM +0900, shalperin wrote: > What I am trying to do is refer to an instance variable both by its=20 > index in some array, and also directly. Having read your example more carefully, I think this does what you really want. This works in reverse from my previous example, by dynamically creating the accessors and storing the variables in an Array: class Referenceable def self.attr_reference(*attrs) @reference ||=3D [] attrs.each {|a| i =3D @reference.size define_method(a) do=20 @reference[i] end define_method((a.to_s + "=3D").intern) do |val| @reference[i] =3D val end @reference << nil } def self.refsize @reference.size end end =20 attr_reader :reference def initialize @reference =3D Array.new(self.class.refsize) end end class Foo < Referenceable attr_reference :a,:b end f =3D Foo.new f.a =3D 1 f.b =3D 2 f.reference =3D> [1,2] f.reference[0] =3D 'test' f.a =3D> 'test' regards, Ed --s2ZSL+KKDSLx8OML Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFDXnt0nhUz11p9MSARAmPfAJ9VT+unFdMcaErpqF8MhrBEdOp+BgCePCpV /+vfMHmqLmvZWumni5kTpOE= =RPYI -----END PGP SIGNATURE----- --s2ZSL+KKDSLx8OML--