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 `<main>'
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.
On Nov 27, 2013 6:52 AM, "Love U Ruby" <lists@ruby-forum.com> wrote:
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 `<main>'