--X1bOJ3K7DJ5YkBrT
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.

How about this? =20

class Referenceable
  def self.attr_reference(*attrs)
    @vars ||=3D []
    @vars +=3D attrs
    attr_accessor *attrs
    v =3D @vars
    define_method(:reference) do |i|
      self.send(v[i])
    end
  end
end

class Foo < Referenceable
  attr_reference :a,:b
end

f =3D Foo.new
f.a =3D 'this is a'
f.b =3D 'this is b'
f.reference(0)
=3D> 'this is a'
f.reference(1)
=3D> 'this is b'

regards,
Ed

--X1bOJ3K7DJ5YkBrT
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)

iD8DBQFDXnfAnhUz11p9MSARApVAAJ94aSdIzyPuL8B4/LKOiRNzbVLI0ACgnlMx
glMm7LQgge9YldGv85Bb0/4=
=wwHV
-----END PGP SIGNATURE-----

--X1bOJ3K7DJ5YkBrT--

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.

How about this? =20

class Referenceable
  def self.attr_reference(*attrs)
    @vars ||=3D []
    @vars +=3D attrs
    attr_accessor *attrs
    v =3D @vars
    define_method(:reference) do |i|
      self.send(v[i])
    end
  end
end

class Foo < Referenceable
  attr_reference :a,:b
end

f =3D Foo.new
f.a =3D 'this is a'
f.b =3D 'this is b'
f.reference(0)
=3D> 'this is a'
f.reference(1)
=3D> 'this is b'

regards,
Ed
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDXnfAnhUz11p9MSARApVAAJ94aSdIzyPuL8B4/LKOiRNzbVLI0ACgnlMx
glMm7LQgge9YldGv85Bb0/4=
=wwHV
-----END PGP SIGNATURE-----