Hey,

While what you've said is true for `class_eval` contexts (that they 
behave like constants) it doesn't explain the following behaviour, which 
the OP may or may not have been getting at:

class C; end

c = C.new

class << c
  @@var = 20
end

#=> Warning: class variable access from toplevel singleton method

It appears that the class var is defined on Object. In fact it's working 
lexically here too:

$c = C.new

module J
  class << $c; @@var = 30; end
end

J.instance_variables #=> [:@@var]

Pretty weird

Gary Wright wrote in post #985860:
> On Mar 6, 2011, at 7:55 PM, JP Billaud wrote:
>
>> Actually it seems that for some reasons the @@test class variables ended
>> up in the Object class:
>>
>> puts Object.class_variables
>>> @@test
>>
>> Anyway I am still confused about this... Help would very appreciated.
>
> The snarky answer is: "Don't use class variables".  They really are hard
> to understand and generally don't have the semantics you expect or need.
>
> The non-snarky answer is that when you do:
>
> egA.class_eval do
>  @@test = 5
> end
>
> Ruby does *not* evaluate '@@test' relative to self, which would be egA,
> but instead evaluates it relative to the lexical scope that is in
> effect, which in your code is the top level scope.  Class variables
> evaluated at the top level scope are instantiated within Object.
>
> Most people expect class variables to only be visible to the class they
> are instantiated in but they are actually visible to their 'home' class
> as well as all subclasses.  Since every class is a subclass of Object,
> in your example, @@test becomes visible in every class and in every
> instance.
>
> Gary Wright

-- 
Posted via http://www.ruby-forum.com/.