--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 
> 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 ||= []
    attrs.each {|a|
      i = @reference.size
      define_method(a) do 
        @reference[i]
      end
      define_method((a.to_s + "=").intern) do |val|
        @reference[i] = val
      end
      @reference << nil
    }
    def self.refsize
      @reference.size
    end
  end
  
  attr_reader :reference
  def initialize
    @reference = Array.new(self.class.refsize)
  end
end

class Foo < Referenceable
  attr_reference :a,:b
end

f = Foo.new
f.a = 1
f.b = 2
f.reference
=> [1,2]
f.reference[0] = 'test'
f.a
=> '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
/+vfMHmqLmvZWumni5kTpOEYI
-----END PGP SIGNATURE-----

--s2ZSL+KKDSLx8OML--