![]() |
attr_accessor for class variable?
Hi,
Is there an equivalent for attr_acessor for the class varialbles? or I have to write a function each time I wish to exppse the class varaible to other clasess? Thanks you very much Sayoyo -- Posted via http://www.ruby-forum.com/. |
Re: attr_accessor for class variable?
Em Monday 05 May 2008, sayoyo Sayoyo escreveu:
> Hi, > > Is there an equivalent for attr_acessor for the class varialbles? or I > have to write a function each time I wish to exppse the class varaible > to other clasess? > attr_acessor applies on class variables, doesn't it? Sorry my poor English. Best regards, -- Davi Vidal -- E-mail: davividal@siscompar.com.br MSN : davividal@msn.com GTalk : davividal@gmail.com Skype : davi vidal YIM : davi_vidal ICQ : 138815296 |
Re: attr_accessor for class variable?
Hi --
On Tue, 6 May 2008, Davi Vidal wrote: > Em Monday 05 May 2008, sayoyo Sayoyo escreveu: >> Hi, >> >> Is there an equivalent for attr_acessor for the class varialbles? or I >> have to write a function each time I wish to exppse the class varaible >> to other clasess? >> > > attr_acessor applies on class variables, doesn't it? No, it's a class method that creates instance methods wrapped around instance variables (read/write methods). David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates! |
Re: attr_accessor for class variable?
On 05.05.2008 17:03, sayoyo Sayoyo wrote:
> Is there an equivalent for attr_acessor for the class varialbles? or I > have to write a function each time I wish to exppse the class varaible > to other clasess? You can just use attr_* family of methods - but you need to apply them to a different object, namely the singleton class of the class object: irb(main):001:0> class Foo irb(main):002:1> class<<self irb(main):003:2> attr_accessor :bar irb(main):004:2> end irb(main):005:1> end => nil irb(main):006:0> Foo.bar = 10 => 10 irb(main):007:0> Foo.bar => 10 irb(main):008:0> Kind regards robert |
Re: attr_accessor for class variable?
Hi --
On Tue, 6 May 2008, sayoyo Sayoyo wrote: > Hi, > > Is there an equivalent for attr_acessor for the class varialbles? or I > have to write a function each time I wish to exppse the class varaible > to other clasess? In Rails there's a "cattr_*" set of methods that wrap class variables. "cattr" is, in my opinion, a bad choice of name, since it implies that it's representing an "attribute" of an object, whereas class variables are visible to many objects and therefore not suitable for representing attributes. One thing you might consider is creating regular accessor methods for the class object, which will use instance variables belonging to the class: class C class << self attr_accessor :x end end C.x = 1 etc. Unless there's some reason you specifically need class variables, that's a cleaner and more accurate way for a class object to maintain and expose state. David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates! |
Re: attr_accessor for class variable?
On Mon, May 5, 2008 at 5:35 PM, Robert Klemme
<shortcutter@googlemail.com> wrote: > You can just use attr_* family of methods - but you need to apply them to a > different object, namely the singleton class of the class object: Robert you just defined accessors to class instance variables, not class variables. Of course one should always use class instance variables instead of class variables. I believe that class variables are evil and class instance variables are good ;). If however class instance variables are used and there is nothing OP can do about it, he can implement the caccessor methods as in Rails. HTH Robert -- http://ruby-smalltalk.blogspot.com/ --- Whereof one cannot speak, thereof one must be silent. Ludwig Wittgenstein |
Re: attr_accessor for class variable?
Hi --
On Wed, 7 May 2008, Robert Dober wrote: > On Mon, May 5, 2008 at 5:35 PM, Robert Klemme > <shortcutter@googlemail.com> wrote: > >> You can just use attr_* family of methods - but you need to apply them to a >> different object, namely the singleton class of the class object: > Robert you just defined accessors to class instance variables, not > class variables. I think Robert was suggesting not using class variables. > Of course one should always use class instance variables instead of > class variables. I believe that class variables are evil and class > instance variables are good ;). > > If however class instance variables are used and there is nothing OP I think you mean class variables :-) > can do about it, he can implement the caccessor methods as in Rails. David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates! |
Re: attr_accessor for class variable?
On May 6, 2008, at 1:45 PM, Shot (Piotr Szotkowski) wrote: > Is this the canonical way to have config objects in applications? > > So far I=92m stuck with > > > > module ArtDecomp class Config > > include Singleton > > attr_accessor :debug, :qu_method, :qv_method, :silicone > > def initialize > @debug =3D false > @qu_method =3D :graph_merger > @qv_method =3D :graph_merger > @silicone =3D Set[Arch[4,2], Arch[5,1]] > end > > def max_pins > @silicone.map{|arch| arch.pins}.max > end > > def max_pons > @silicone.map{|arch| arch.pons}.max > end > > def reset > initialize > end > > end end require 'configuration' # gem install configuration Configuration.for :art_decomp do qu_method :graph_merger qv_method :graph_merger end now simply require or load that file and you can use c =3D Configuration.for :art_decomp p c.qv_method = http://codeforpeople.com/lib/ruby/co...-0.0.5/README= a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being =20 better. simply reflect on that. h.h. the 14th dalai lama |
Re: attr_accessor for class variable?
On Wed, May 7, 2008 at 1:11 AM, David A. Black <db
> I think Robert was suggesting not using class variables. Not so sure David I almost had written the same reply, comes so natural I feel. Anyway I felt it was necessary to clarify for OP's state of mind. ;) <snip> > > If however class instance variables are used and there is nothing OP > > > > I think you mean class variables :-) Here of course you are spot on :) R. |
| All times are GMT. The time now is 04:14 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.