From: Farrel Lifson Date: 2006-10-23T19:33:22+09:00 Subject: Re: newbie: class attribute accessors? On 23/10/06, 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 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