Is using attr_accessor for class variables possible?

Discussion in 'Ruby' started by Gabriel Dragffy, Oct 7, 2007.

  1. I know that attr_accessor works for accessing instance variables, but
    it doesn't seem to work for class ones. What is the best way to set/
    retrieve the value of a class variable?

    Many thanks

    Gabriel
     
    Gabriel Dragffy, Oct 7, 2007
    #1
    1. Advertising

  2. Hi --

    On Sun, 7 Oct 2007, Gabriel Dragffy wrote:

    > I know that attr_accessor works for accessing instance variables, but it
    > doesn't seem to work for class ones. What is the best way to set/retrieve the
    > value of a class variable?


    @@var = 1
    @@var

    :)

    If you want to wrap them in methods:

    class C
    def C.var
    @@var
    end

    def C.var=(x)
    @@var = x
    end
    end

    In Rails, there's a thing called "cattr" that does this automatically:

    class C
    cattr_acccessor :var
    end

    It's a little misleading, though. The term "attribute" (or attr)
    refers to an attribute or property of an object. Class variables are
    very promiscuous: they're shared among many objects (a class, its
    descendants, all instances of all of those classes), so a class
    variable is not really the right choice for an "attribute", and "attr"
    is not the best name for wrappers around class variables.

    If you want to represent state per class, the best way is to give your
    class an instance variable or accessor:

    class C
    class << self # C's singleton class
    attr_accessor :var
    end
    end


    David

    --
    Upcoming training from Ruby Power and Light, LLC:
    * Intro to Ruby on Rails, Edison, NJ, October 23-26
    * Advancing with Rails, Edison, NJ, November 6-9
    Both taught by David A. Black.
    See http://www.rubypal.com for more info!
     
    David A. Black, Oct 7, 2007
    #2
    1. Advertising

  3. On 7 Oct 2007, at 13:39, David A. Black wrote:

    > Hi --
    >
    > On Sun, 7 Oct 2007, Gabriel Dragffy wrote:
    >
    >> I know that attr_accessor works for accessing instance variables,
    >> but it doesn't seem to work for class ones. What is the best way
    >> to set/retrieve the value of a class variable?

    >
    > @@var = 1
    > @@var
    >


    Good point, I mean access the class variable from outside the class
    (above the class).

    > :)
    >
    > If you want to wrap them in methods:
    >
    > class C
    > def C.var
    > @@var
    > end
    >
    > def C.var=(x)
    > @@var = x
    > end
    > end
    >


    I was wondering if I should do this. I just remember attr_accessor
    was a short cut for this, but only for instance variables.

    > In Rails, there's a thing called "cattr" that does this automatically:
    >
    > class C
    > cattr_acccessor :var
    > end
    >
    > It's a little misleading, though. The term "attribute" (or attr)
    > refers to an attribute or property of an object. Class variables are
    > very promiscuous: they're shared among many objects (a class, its
    > descendants, all instances of all of those classes), so a class
    > variable is not really the right choice for an "attribute", and "attr"
    > is not the best name for wrappers around class variables.
    >
    > If you want to represent state per class, the best way is to give your
    > class an instance variable or accessor:
    >
    > class C
    > class << self # C's singleton class
    > attr_accessor :var
    > end
    > end


    I read about singletons in Pragmatic Programming, not entirely sure
    how to make use of it though

    Thank you very much for your advice.

    Regards

    Gabe
     
    Gabriel Dragffy, Oct 7, 2007
    #3
  4. Hi,

    Am Sonntag, 07. Okt 2007, 21:39:24 +0900 schrieb David A. Black:
    > If you want to represent state per class, the best way is to give your
    > class an instance variable or accessor:
    >
    > class C
    > class << self # C's singleton class
    > attr_accessor :var
    > end
    > end


    I do this all the time. But:

    class D < C ; end

    Always keep in mind that `D.var' will be something different
    than `C.var'.

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Oct 7, 2007
    #4
  5. Hi --

    On Sun, 7 Oct 2007, Bertram Scharpf wrote:

    > Hi,
    >
    > Am Sonntag, 07. Okt 2007, 21:39:24 +0900 schrieb David A. Black:
    >> If you want to represent state per class, the best way is to give your
    >> class an instance variable or accessor:
    >>
    >> class C
    >> class << self # C's singleton class
    >> attr_accessor :var
    >> end
    >> end

    >
    > I do this all the time. But:
    >
    > class D < C ; end
    >
    > Always keep in mind that `D.var' will be something different
    > than `C.var'.


    That's the point of attr_accessor: to give easy access to per-object
    state.


    David

    --
    Upcoming training from Ruby Power and Light, LLC:
    * Intro to Ruby on Rails, Edison, NJ, October 23-26
    * Advancing with Rails, Edison, NJ, November 6-9
    Both taught by David A. Black.
    See http://www.rubypal.com for more info!
     
    David A. Black, Oct 7, 2007
    #5
  6. Hi,

    Am Sonntag, 07. Okt 2007, 22:56:54 +0900 schrieb David A. Black:
    > On Sun, 7 Oct 2007, Bertram Scharpf wrote:
    >> Am Sonntag, 07. Okt 2007, 21:39:24 +0900 schrieb David A. Black:
    >>> If you want to represent state per class, the best way is to give your
    >>> class an instance variable or accessor:
    >>>
    >>> class C
    >>> class << self # C's singleton class
    >>> attr_accessor :var
    >>> end
    >>> end

    >>
    >> I do this all the time. But:
    >>
    >> class D < C ; end
    >>
    >> Always keep in mind that `D.var' will be something different
    >> than `C.var'.

    >
    > That's the point of attr_accessor: to give easy access to per-object
    > state.


    About a month ago I found myself doing even this:

    class C
    @x = "x-default"
    class <<self
    def x ; @x or superclass.x ; end
    end
    end

    class D < C ; end
    class E < D ; @x = "x-special" ; end

    puts C.x
    puts D.x
    puts E.x

    I still don't know what is better: using stable instance
    methods (same value on every call) or doing it this way.

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Oct 7, 2007
    #6
    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:
    102
    Jim Cain
    Jul 18, 2003
  2. David Garamond
    Replies:
    1
    Views:
    103
    Dan Doel
    Jan 25, 2004
  3. Jack, Paul
    Replies:
    2
    Views:
    114
    Robert Klemme
    Dec 16, 2004
  4. Giles Bowkett
    Replies:
    9
    Views:
    167
    Ben Tompkins
    Sep 12, 2007
  5. sayoyo Sayoyo

    attr_accessor for class variable?

    sayoyo Sayoyo, May 5, 2008, in forum: Ruby
    Replies:
    8
    Views:
    455
    Robert Dober
    May 7, 2008
Loading...

Share This Page