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

attr_acessor applies on class variables, doesn't it?

  Sorry my poor English.

  Best regards,

···

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?

--
Davi Vidal
--
E-mail: davividal@siscompar.com.br
MSN : davividal@msn.com
GTalk : davividal@gmail.com
Skype : davi vidal
YIM : davi_vidal
ICQ : 138815296

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

···

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?

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

···

On Tue, 6 May 2008, sayoyo Sayoyo wrote:

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

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!

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

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

···

On 05.05.2008 17:03, sayoyo Sayoyo wrote:

--
I actually am proud of the stacking status bar. My
contribution to the fast-paced world of Curses-based
interfaces, we'll call it. -- William Morgan, sup-talk

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

···

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:

--
http://ruby-smalltalk.blogspot.com/

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Hi --

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 Wed, 7 May 2008, Robert Dober wrote:

On Mon, May 5, 2008 at 5:35 PM, Robert Klemme > <shortcutter@googlemail.com> wrote:

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

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

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

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:

<snip>

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