From: "David A. Black" Date: 2005-09-21T20:57:05+09:00 Subject: Re: Problem with constants Hi -- On Wed, 21 Sep 2005, 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. Actually A is a constant, not a local variable, and method definitions aren't closures. I'm pretty sure that what's happening is that the constant references are being resolved at compile-time, so that by the time Y.a is run, the reference inside X.a has been permanently resolved to X::A. See what happens when you make this change: def X.a const_get("A") end Now the constant is being resolved dynamically, and you'll get 1/2/3 in the output. David -- David A. Black dblack@wobblini.net