module A def greet "Hello from A" end end module B def greet "Hello from B" end end class C include A include B end puts C.new.greet
In Ruby, when multiple modules are included, the last included module is checked first for methods. Here, module B is included after A, so greet from B is called.
Ruby first looks in the class itself, then in the modules included in that class (checking the last included module first), then in the superclass, and finally in the modules included in the superclass.
module M def hello "Hello from M" end end class X include M def hello "Hello from X" end end class Y < X prepend M end puts Y.new.hello
Using prepend inserts the module before the class in the method lookup chain. So hello from module M is called before the class X's method.
module M def foo "foo from M" end end class A include M end class B < A def foo super + " and foo from B" end end obj = B.new puts obj.foo
super starts method lookup from the superclass and follows its full ancestor chain, including modules included there.foo is defined in module M, included in A. When B#foo calls super, lookup starts at A, which has no foo directly but includes M, so M#foo is found and executed, returning "foo from M". This is concatenated with " and foo from B".
module M1 def call "M1" end end module M2 def call "M2" end end class Base def call "Base" end end class Derived < Base include M1 prepend M2 end puts Derived.new.call
Prepending M2 places it before Derived in the lookup chain, so call from M2 is found first. Included modules like M1 come after the class itself.