attr_accessor for class variable?

Discussion in 'Ruby' started by sayoyo Sayoyo, May 5, 2008.

  1. 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/.
    sayoyo Sayoyo, May 5, 2008
    #1
    1. Advertising

  2. sayoyo Sayoyo

    Davi Vidal Guest

    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:
    MSN :
    GTalk :
    Skype : davi vidal
    YIM : davi_vidal
    ICQ : 138815296
    Davi Vidal, May 5, 2008
    #2
    1. Advertising

  3. 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!
    David A. Black, May 5, 2008
    #3
  4. 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 Klemme, May 5, 2008
    #4
  5. 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!
    David A. Black, May 5, 2008
    #5
  6. sayoyo Sayoyo

    Robert Dober Guest

    On Mon, May 5, 2008 at 5:35 PM, Robert Klemme
    <> 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
    Robert Dober, May 6, 2008
    #6
  7. Hi --

    On Wed, 7 May 2008, Robert Dober wrote:

    > On Mon, May 5, 2008 at 5:35 PM, Robert Klemme
    > <> 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!
    David A. Black, May 7, 2008
    #7
  8. sayoyo Sayoyo

    ara.t.howard Guest

    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/configuration/configuration-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
    ara.t.howard, May 7, 2008
    #8
  9. sayoyo Sayoyo

    Robert Dober Guest

    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.
    Robert Dober, May 7, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Cain
    Replies:
    0
    Views:
    81
    Jim Cain
    Jul 18, 2003
  2. David Garamond
    Replies:
    1
    Views:
    84
    Dan Doel
    Jan 25, 2004
  3. Jack, Paul
    Replies:
    2
    Views:
    99
    Robert Klemme
    Dec 16, 2004
  4. Giles Bowkett
    Replies:
    9
    Views:
    151
    Ben Tompkins
    Sep 12, 2007
  5. Gabriel Dragffy
    Replies:
    5
    Views:
    105
    Bertram Scharpf
    Oct 7, 2007
Loading...

Share This Page