Hi --

On Mon, 20 Feb 2006, Tony Mobily wrote:

> Dear Tony,
>
> [OK, I am gonna answer myself]
>
> The main difference actually makes a lot of sense.
>
> If you change a class constant (which you shouldn't do), then all of the 
> classes which inherited from it will see their constant changed as well.
>
> Class variables, on the other hand, are allocated for the class itself. This 
> means that each class (or sub-class) will have its own class variables.
>
> Here is some code to clarify the concept.
>
> -----------------------------------------------
> #!/usr/local/bin/ruby -w
>
> class TonyCantCode
>       @@internal=10
>
>       def TonyCantCode::something
>               return @@internal
>       end
>
>       def TonyCantCode::something=(what)
>               @@internal=what
>       end
>
>
> end
>
> class TonyReallyCantCode
>       Something=20
> end
>
> class TonyCantCode2 < TonyCantCode
> end
>
> class TonyReallyCantCode2 < TonyReallyCantCode
>       Something=20
> end
>
>
> p TonyCantCode::something
> p TonyReallyCantCode::Something
>
> TonyCantCode::something=30
> TonyReallyCantCode::Something=40
>
> p TonyCantCode::something
> p TonyReallyCantCode::Something
>
> p TonyCantCode2::something
> p TonyReallyCantCode2::Something
>
>
> RESULT:
>
> $ ./vars.rb
> 10
> 20
> ./vars.rb:33: warning: already initialized constant Something
> 30
> 40
> 30
> 20 <-- !!!

Have you tried this?

    class A
      @@var = 10
      def self.var
        @@var
      end
    end

    class B < A
      @@var = 20
    end

    p A.var

:-)


David

-- 
David A. Black (dblack / wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black