Translate

Tuesday, 22 October 2013

Diffrence between include and extend



When you're "including" a module, the module is included as if the methods were defined at the class that's including them, you could say that it's copying the methods to the including class.
When you're "extending" a module, you're saying "add the methods of this module to this specific instance". When you're inside a class definition and say "extend" the "instance" is the class object itself, 
module MyModule
  def foo
    puts "foo called"
  end
end

class A
end

object = A.new
object.extend MyModule

object.foo #prints "foo called"    
So, it's not exactly a class method, but a method to the "instance" which you called "extend". As you're doing it inside a class definition and the instance in there is the class itself, it "looks like" a class method.