Mixins
suggest changeMixins 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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents