On Mar 22, 10:47 am, SunRaySon <sunray... / gmail.com> wrote:
> I am expecting it to print ModuleA::foo because the method foo in
> ModuleA would overwrite the method foo when I include ModuleA  after
> reopening the class A later. Am I missing something here, can anyone
> help me understand this.

I suppose the question is "Why were you expecting the module to
overwrite the instance methods defined in the class?"

Modules included in a class insert themselves after the class itself,
but before the ancestor of the class:

module Mod
  def foo; "Mod#foo"; end
end

class A
  def foo; "A#foo"; end
end

class B < A
  include Mod
end

p A.new.foo, B.new.foo
"A#foo"
"Mod#foo"

This diagram might help (although perhaps I could improve it a little
bit to clarify this particular case):

http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png