[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

newbie: class attribute accessors?

Diego Virasoro

10/23/2006 9:35:00 AM

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

7 Answers

Paul Lutus

10/23/2006 9:55:00 AM

0

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

Diego Virasoro

10/23/2006 10:24:00 AM

0

> 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

Farrel Lifson

10/23/2006 10:33:00 AM

0

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

dblack

10/23/2006 11:23:00 AM

0

Norgg

10/23/2006 11:23:00 AM

0

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

Paul Lutus

10/23/2006 11:24:00 AM

0

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

Eero Saynatkari

10/23/2006 4:39:00 PM

0

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

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'