Mixins in Ruby

MIXIN:

Mixin is a class that is mixed with a module. In other words the implementation of the class and module are joined, intertwined, combined, etc. A mixin is a different mechanism to the extend construct used to add concrete implementation to a class. With a mixin you can extend from a module instead of a class.

MODULES:

Module is a degenerate abstract class. A module can’t be instantiated and no class can directly extend it but a module can fully implement methods. A class can leverage the implementation of a module by including the module’s methods. A module can define methods that can be shared in different and separate classes either at the class or instance level.

Example:

module Abstract
  def run
    @value * 2
  end
end

class Child

  include Abstract

  def initialize(value)
    @value = value
  end

end

c = Child.new(20)
puts c.run

Links:

http://stackoverflow.com/questions/1282864/ruby-inheritance-vs-mixins

http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/

Must Read Link:

http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/

==> This link would also help you understand what does self.included and class_eval and instance_eval does??