attr_accessor for class variable?

S

sayoyo Sayoyo

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
 
D

Davi Vidal

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: (e-mail address removed)
MSN : (e-mail address removed)
GTalk : (e-mail address removed)
Skype : davi vidal
YIM : davi_vidal
ICQ : 138815296
 
D

David A. Black

Hi --

Em Monday 05 May 2008, sayoyo Sayoyo escreveu:

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!
 
R

Robert Klemme

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
 
D

David A. Black

Hi --

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!
 
R

Robert Dober

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
 
D

David A. Black

Hi --

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!
 
A

ara.t.howard

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/configuration/configuration-0.0.5/README=


a @ http://codeforpeople.com/
 
R

Robert Dober

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. ;)

I think you mean class variables :)
Here of course you are spot on :)
R.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top