Mixins

suggest change

Mixins are a way to get something similar to multiple inheritance. They include methods defined in a module into a class, as instance methods or as class methods. The example below does both.

module SampleModule

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods

    def method_static
      puts "This is a static method"
    end

  end

  def insta_method
    puts "This is an instance method"
  end

end

class SampleClass
  include SampleModule
end

sc = SampleClass.new

sc.insta_method
# prints: This is an instance method

sc.class.method_static
# prints: This is a static method

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents