Professional Web Applications Themes

inheritence of class vars - Ruby

am i the only one suprised by this? ~/eg/ruby > cat class_vars.rb class A class << self def x= value x = value end def x x end end end class B < A; end A.x = 'forty-two' B.x = 42 p A.x p B.x class A class << self def x= value x = value end def x x end end end A.x = 'forty-two' B.x = 42 p A.x p B.x ~/eg/ruby > ruby class_vars.rb 42 42 "forty-two" 42 i see it, but don't believe it... is it _correct_ to say that: iff you want a subclass to ...

  1. #1

    Default inheritence of class vars


    am i the only one suprised by this?

    ~/eg/ruby > cat class_vars.rb

    class A
    class << self
    def x= value
    x = value
    end
    def x
    x
    end
    end
    end
    class B < A; end

    A.x = 'forty-two'
    B.x = 42
    p A.x
    p B.x

    class A
    class << self
    def x= value
    x = value
    end
    def x
    x
    end
    end
    end

    A.x = 'forty-two'
    B.x = 42
    p A.x
    p B.x


    ~/eg/ruby > ruby class_vars.rb

    42
    42
    "forty-two"
    42

    i see it, but don't believe it... is it _correct_ to say that:

    iff you want a subclass to have it's own copy of a 'class var' - use instance vars

    * by 'class var' i mean _not_ an instance var. obviously there are two
    types

    i don't know how i made it this long without that biting me, doh!

    -a
    --
    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
    ================================================== =============================

    Ara.T.Howard Guest

  2. #2

    Default Re: inheritence of class vars

    David A. Black wrote: 

    The per-hierarchy thing was never very clear. Just _which_ hierarchy a
    variable belongs to depends on the order in which it is first assigned:

    class A; end
    class B < A; end

    # Try switching the following two lines!
    class A; x = "A"; end
    class B; x = "B"; end

    class A; puts "In A, x = #{x}"; end
    class B; puts "In B, x = #{x}"; end

    Before switching the lines, there is one x for the hierarchy
    consisting of A and B. After switching, there is one x for A and
    _another_ x for B and its descendants.

    It makes sense, but it's an easy place to get tripped.


    Joel Guest

  3. #3

    Default Re: inheritence of class vars

    On Sat, Feb 14, 2004 at 10:44:04AM +0900, David A. Black wrote: 

    Might be me being stupid.. but if these new rules are in place, what will
    the difference between class variables and instance variables (of class
    objects) be?

    The only thing coming to my mind, is that class variables are easily
    accessed from instance methods, while you need to do
    "self.class.class_eval{my_class_instance_var} " for class-object instance
    variables.

    BTW: Just for the sake of being curious... is it possible to define
    class-variables of the class Class?

    greetings, Florian Pflug



    Florian Guest

  4. #4

    Default Re: inheritence of class vars

    On Sat, Feb 14, 2004 at 10:44:04AM +0900, David A. Black wrote: 

    I think making class variables an alias for class instance variables
    would be one alternate solution (they would just be a shortcut for
    accessing the class instance variable from inside instance methods).

    Paul



    Paul Guest

Similar Threads

  1. Replies: 1
    Last Post: November 24th, 10:11 AM
  2. #26364 [Bgs->Opn]: initializing class in other class vars value problem
    By brightone at o2 dot pl in forum PHP Development
    Replies: 0
    Last Post: November 24th, 10:04 AM
  3. #26364 [NEW]: initializing class in other class vars value problem
    By brightone at o2 dot pl in forum PHP Development
    Replies: 0
    Last Post: November 23rd, 03:06 PM
  4. Problem with exception class and instance vars
    By Bennett, Patrick in forum Ruby
    Replies: 2
    Last Post: November 13th, 07:27 PM
  5. [PHP] vars between instantiate class...
    By Raditha Dissanayake in forum PHP Development
    Replies: 1
    Last Post: September 4th, 05:02 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139