From: HaPK Date: 2005-09-21T20:59:20+09:00 Subject: Re: Problem with constants HaPK wrote: > Kroeger Simon (ext) wrote: > >> I'm puzzled: >> ----------------------- >> class X >> A = 1 >> def X.a >> A >> end >> end >> >> class Y < X >> A = 2 >> end >> >> class Z < X >> A = 3 >> def Z.a >> A >> end >> end >> >> p X.a #=> 1 >> p Y.a #=> 1 >> p Z.a #=> 3 >> ----------------------- >> (ruby 1.8.2 (2004-12-25) [i386-mswin32]) >> >> Shouldn't this yield at least a warning? >> Is this a feature? >> >> > Since A is not a class variable (it's a local variable) it's saved in > "X.a" closure. In Y class it doesn't exist, so it can't influence on > "X.a" method. > > PS I hope my crappy english doesn't mess things too much. Ooops... sorry.. Didn't get you right. Indeed, seems like a bug with constants assignment. class X A = 1 end class Y < X A = 2 end class Z < X end X.constants => 'A' Y.constants => 'A' Z.constants => 'A' # Z inherits constants from its parent X::A = 3 Z::A => 3 # changes in superclass populated to subclasses Z::A = 4 X::A => 3 # changes in subclass remains local to subclass Seems like assignments are made without lookup in superclasses.