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
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
Diego Virasoro 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.
--
Paul Lutus
http://www.arachnoid.com
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
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
On 23/10/06, Diego Virasoro <Diego.Virasoro@gmail.com> wrote:
> 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 = 4attr_accessor :name
endx = Animal.new
...
puts x.nameHowever, 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
Hi --
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 = 4attr_accessor :name
endx = Animal.new
...
puts x.nameHowever, 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
On Mon, 23 Oct 2006, Diego Virasoro wrote:
--
David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
Diego Virasoro 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.
--
Paul Lutus
http://www.arachnoid.com
Diego Virasoro wrote:
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 = 4attr_accessor :name
endx = Animal.new
...
puts x.nameHowever, 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
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'
On 2006.10.23 20:25, John Turner wrote:
Diego Virasoro wrote:
>>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
>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