A question about constant search in Ruby. In the code below,

class M
  CONST = "Hello, world"
class M
  def self.say
p M.say # "Helo, world"

Why is this standard output possible?

My thoughts

The constant search of Ruby will look back at "1: Lexical scope" and then go back up "2: Inheritance chain".

Constant CONST is not defined in the lexical scope where the standard output method is defined. (The M class is defined twice, but the first definition and redefinition recognize it as a different "lexical scope".)

Also, the method defined in the M class is a singular method, and I think it will be a method belonging to#, which is a metaclass (?) of M, not the M class itself. The

#and M class have a "class-object" relationship, but not a "super class-child class" relationship, so#cannot be used to refer to the constant CONST defined in the M class.

Therefore, you shouldn't be able to find a constant that doesn't exist in either "lexical scope" or "superclass".

I think it is

... but I can actually refer to it, so I think that either way of thinking (recognition of lexical scope or singular class and singular method) is wrong. I'm sorry, but I'm not sure which one is wrong or whether there is another serious misunderstanding.

Can you tell me which way of thinking is wrong?