Newbie: class attribute accessors?

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

Diego V. wrote:

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.

Please explain. Use an example.

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

Hi –

On Mon, 23 Oct 2006, Diego V. wrote:

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

David

On 23/10/06, Diego V. removed_email[email protected] wrote:

end

Thank you

Diego V.

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

Diego V. wrote:

/ …

However, I would like to do something similar for class variables. So
for example in the above example, for @@NumberOfLegs.

Is this possible?

I don’t think so (and I am certainly open to correction), and I think I
know
the reason. A class variable belongs to the class, not to any particular
instance. The accessor shortcuts are meant to make instance variables
available, but you need to think what would happen if any instance could
change the class variables.

Class variables are meant to hold values the entire class hold in
common.
These variables should not ordinarily be modifiable by any particular
instance. Also, if accessor functions existed for class variables, this
would essentially allow them to be modified globally, by anyone, without
any of the usual protections. Further, such an accessor feature would
exist
at the class level, just like the variables it modified, so no instance
would need to be created.

Again, I could be wrong about this.

Diego V. wrote:

end

Thank you

Diego V.

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

On 2006.10.23 20:25, John T. wrote:

attr_accessor :name
Is this possible?
@names = [“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 = 5; end
class B < A; @@a = 4; end
class C < A; @@a = 3; end
class D < B; @@a = 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 = 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 = 1; end
class F < E; end

p E.send ‘instance_variable_get’, ‘@a
p F.send ‘instance_variable_get’, ‘@a

class F; @a = 2; end

p E.send ‘instance_variable_get’, ‘@a
p F.send ‘instance_variable_get’, ‘@a

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs