This issue is not specific to opening a singleton class (class <<), but applies to any case where the class being opened is a singleton class. You get the same output for:
I'm not sure if this class variable behavior is a bug or spec. If you consider it a bug, and say that class variables set inside a singleton class definition are set on the singleton class and not the outer nesting, then you would probably break the following code (and I'm guessing class << self is much more common than class << some_other_object):
moduleMod1class<<self@@cv=1enddefa@@cvendend
FWIW, Ruby does allow you to set class variables on singleton classes via class_variable_get/class_variable_set, but you cannot access them via normal means:
moduleMod1singleton_class.class_variable_set(:@@cv,1)class<<selfpclass_variable_get(:@@cv)@@cvendend# 1# NameError: uninitialized class variable @@cv in Mod1
Class variable lookup just ignores singleton classes currently.
I assume this is intentional behavior so one can use the same variable in singleton methods (when defined under class<<self) and instance methods.
Class variable lookup going from singleton class to actual class appears to be intentional behavior if you look at the code. The class_variable_get bug is being addressed in #8297. Assigning to matz for confirmation that this behavior is expected.
Please do not set the status to Feedback if you have responded. Feedback status means that committers are waiting for feedback from you. You can reset the status to Open if you disagree with the initial closing of an issue.
I am not a big fan of this behavior, but I don't think class variables are worth breaking the existing code for the sake of preference.
So I reject the proposal.