--J+xDcZ1j08+V/OfU
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Diego said:
automatically produce the accessors. For example:
class Dog
@name
@@NumberOfLegs =3D 4
attr_accessor :name
end
x =3D Animal.new
...
puts x.name
However, I would like to do something similar for class variables. So
for example in the above example, for @@NumberOfLegs.
Is this possible?
Thank you
Diego Virasoro
=20
You can do this for instance variables of a class, not sure there are=20
equivalents for actual class variables though:
=20
class Dog
@names =3D ["Fido", "Rex"]
class << self
attr_accessor :names
end
end
puts Dog.names
Please note that it is often better to use class instance variables
(@var at the class scope) rather than class variables (@@var):
class A; @@a =3D 5; end
class B < A; @@a =3D 4; end
class C < A; @@a =3D 3; end
class D < B; @@a =3D 2; end
p A.send 'class_variable_get', "@@a"
p B.send 'class_variable_get', "@@a"
p C.send 'class_variable_get', "@@a"
p D.send 'class_variable_get', "@@a"
class C; @@a =3D 4; end
p A.send 'class_variable_get', "@@a"
p B.send 'class_variable_get', "@@a"
p C.send 'class_variable_get', "@@a"
p D.send 'class_variable_get', "@@a"
Class instance variables have a simpler inheritance, none:
class E; @a =3D 1; end
class F < E; end
p E.send 'instance_variable_get', '@a'
p F.send 'instance_variable_get', '@a'
class F; @a =3D 2; end
p E.send 'instance_variable_get', '@a'
p F.send 'instance_variable_get', '@a'
--J+xDcZ1j08+V/OfU
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)
iD8DBQFFPO9o7Nh7RM4TrhIRAh7eAJ9wd7faT038x78bWsQVG1yGciACyQCgm7Mo
RlUi5BODUKi2dGhV1+AiE8w=
=tGle
-----END PGP SIGNATURE-----
--J+xDcZ1j08+V/OfU--