newbie: class attribute accessors?

D

Diego Virasoro

Hello,
I was wondering if it is possible to get the automatically generated
accessors (attr_accessor, etc...) for class attributes. From what I
found on the internet, it seems that the usual syntax wouldn't work.

Thanks.

Diego Virasoro
 
D

Diego Virasoro

Please explain. Use an example.
From what I understood, for any instance variable, I can make ruby
automatically produce the accessors. For example:

class Dog
@name
@@NumberOfLegs = 4

attr_accessor :name
end

x = 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
 
F

Farrel Lifson

automatically produce the accessors. For example:

class Dog
@name
@@NumberOfLegs = 4

attr_accessor :name
end

x = 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

I don't know if there is an 'attr_accessor' shortcut but you can do it manually
irb(main):001:0> class Dog
irb(main):002:1> def self.number_of_legs
irb(main):003:2> @@numberOfLegs
irb(main):004:2> end
irb(main):005:1> def self.number_of_legs=(value)
irb(main):006:2> @@numberOfLegs = value.to_int
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> Dog.number_of_legs=5
=> 5
irb(main):010:0> Dog.number_of_legs
=> 5

Farrel
 
D

dblack

Hi --

From what I understood, for any instance variable, I can make ruby
automatically produce the accessors. For example:

class Dog
@name
@@NumberOfLegs = 4

attr_accessor :name
end

x = 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?

See Farrel's answer; you can write wrapper methods. Note, however,
that these are not exactly "attributes". Class variables are shared
among many different objects (all the classes in a hierarchy; all the
instances of all those classes), so they can't represent an attribute
in the normal/usual sense.

You can certainly give your class objects attributes. Just call
attr_accessor in the object's singleton class:

class Dog
class << self
attr_accessor :number_of_legs
end
self.number_of_legs = 4
end

puts Dog.number_of_legs # => 4
Dog.number_of_legs = 10 # well, maybe attr_reader is better :)


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
J

John Turner

Diego said:
automatically produce the accessors. For example:

class Dog
@name
@@NumberOfLegs = 4

attr_accessor :name
end

x = 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

You can do this for instance variables of a class, not sure there are
equivalents for actual class variables though:

class Dog
@names = ["Fido", "Rex"]
class << self
attr_accessor :names
end
end

puts Dog.names
 
E

Eero Saynatkari

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top