Class Variables for subclases...

Discussion in 'Ruby' started by Sonny Chee, Mar 27, 2007.

  1. Sonny Chee

    Sonny Chee Guest

    Hey Guys,

    Check out the following:

    class A
    @@anobject = 'green'
    def fun2
    @@anobject
    end
    end

    class B < A
    @@anobject = 'blue'
    def fun2
    @@anobject
    end
    end

    puts B.new.fun2 # blue
    puts A.new.fun2 # blue

    So it looks like the subclass B is not defining its own class variable
    but reusing A's class variable @@anobject. Is there a different way to
    specify class variables to overcome this issue?

    Sonny.

    --
    Posted via http://www.ruby-forum.com/.
    Sonny Chee, Mar 27, 2007
    #1
    1. Advertising

  2. Sonny Chee

    Sonny Chee Guest

    Yukihiro Matsumoto wrote:

    > You can use constants-as-class-shared technique.


    Thanks, Matz. For my edification, is there a reason why class variables
    behave in this way when subclassed? Is this a feature of the language
    or the Ruby Interpretter?

    Sonny.

    --
    Posted via http://www.ruby-forum.com/.
    Sonny Chee, Mar 27, 2007
    #2
    1. Advertising

  3. Sonny Chee wrote:
    > Hey Guys,
    >
    > Check out the following:
    >
    > class A
    > @@anobject = 'green'
    > def fun2
    > @@anobject
    > end
    > end
    >
    > class B < A
    > @@anobject = 'blue'
    > def fun2
    > @@anobject
    > end
    > end
    >
    > puts B.new.fun2 # blue
    > puts A.new.fun2 # blue
    >
    > So it looks like the subclass B is not defining its own class variable
    > but reusing A's class variable @@anobject. Is there a different way to
    > specify class variables to overcome this issue?
    >
    > Sonny.
    >


    You may find it more suitable to use instance variables in your class
    objects, and write accessors so you can get at them from instances of
    your classes.

    For example:

    class A
    @anobject = 'green'
    def self.anobject
    @anobject
    end
    def fun2
    self.class.anobject
    end
    end

    class B < A
    @anobject = 'blue'
    end

    puts B.new.fun2 # blue
    puts A.new.fun2 # green


    Google (or search the list) for 'ruby class instance variable'.

    I think the 'traits' gem does this in a systematic way, though of course
    it's worth playing around to understand the concept first.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Mar 27, 2007
    #3
  4. Sonny Chee

    Sonny Chee Guest

    Sonny Chee, Mar 27, 2007
    #4
    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. avnrao
    Replies:
    0
    Views:
    3,353
    avnrao
    May 7, 2004
  2. dwok
    Replies:
    7
    Views:
    688
    Hal Rosser
    Mar 4, 2005
  3. Replies:
    10
    Views:
    35,847
    jporter892
    Jun 6, 2011
  4. E11
    Replies:
    1
    Views:
    4,718
    Thomas Weidenfeller
    Oct 12, 2005
  5. Replies:
    9
    Views:
    931
Loading...

Share This Page