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

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

E-mail: [email protected]
MSN : [email protected]
GTalk : [email protected]
Skype : davi vidal
YIM : davi_vidal
ICQ : 138815296

Hi –

On Tue, 6 May 2008, Davi V. 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

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

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

Robert K.:

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

Is this the canonical way to have config objects in applications?

So far I’m stuck with

module ArtDecomp class Config

include Singleton

attr_accessor :debug, :qu_method, :qv_method, :silicone

def initialize
@debug = false
@qu_method = :graph_merger
@qv_method = :graph_merger
@silicone = 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

Hence, my code is either cropped with either Config.instance.debug
ugliness or @config = Config.instance; #…; @config.debug stuff.

I’d like to have a clean Config.debug syntax, while still having the
ability to initialise it (currently with defaults, like above; in the
future with some optional Trollop stuff added).

– Shot

Hi –

On Wed, 7 May 2008, Robert D. wrote:

On Mon, May 5, 2008 at 5:35 PM, Robert K.
[email protected] 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 :slight_smile:

can do about it, he can implement the caccessor methods as in Rails.

David

On Mon, May 5, 2008 at 5:35 PM, Robert K.
[email protected] 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

On May 6, 2008, at 1:45 PM, Shot (Piotr S.) wrote:

attr_accessor :debug, :qu_method, :qv_method, :silicone
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 = Configuration.for :art_decomp
p c.qv_method

http://codeforpeople.com/lib/ruby/configuration/configuration-0.0.5/README

a @ http://codeforpeople.com/

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. :wink:

If however class instance variables are used and there is nothing OP

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