Your problem is that you are mistaken in the meaning of the code.
class << self FOO = :bar end
not equivalent to self.FOO = :bar . This is very different from this. This is equivalent to self.singleton_class.const_set(:FOO, :bar) .
I think you are assuming that class << self means "suggesting there is an implicit" I "in front of everything that I write here" or something like that (maybe you are thinking of JavaScript with ). In fact, this means that we put ourselves in the context of the self singleton class, whose special class is the only instance of the current object. Thus, you define a constant in the Singleton class of the class.
To define a constant in a class, simply write:
class Something FOO = :bar end
Chuck
source share