Why not we able to include module in the parent class from child class?

I am having some confusion with the include method in Ruby, to include
a module in a parent class, which is defined in a child class of that
parent class.

class B
module M
def meth
11
end
end
end

class A < B
end
A.new.meth # undefined method `meth’ for #<A:0x9780bdc> (NoMethodError)

I am fine with above.

class B
module M
def meth
11
end
end
end

class A < B
include M
end
A.new.meth # => 11

still I am fine with this.

Why I am not able to do the below?

class A
include B::M
end

class B < A
module M
def meth
11
end
end
end

~> -:2:in `class:A’: uninitialized constant A::B (NameError)

~> from -:1:in `’

Typing order. When ruby sees you including B::M neither B nor B::M have
been defined yet.

Sent from my phone, so excuse the typos.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs