A simple mixin with include
suggest changemodule SomeMixin
def foo
puts "foo!"
end
end
class Bar
include SomeMixin
def baz
puts "baz!"
end
end
b = Bar.new
b.baz # => "baz!"
b.foo # => "foo!"
# works thanks to the mixin
Now Bar is a mix of its own methods and the methods from SomeMixin.
How a mixin is used depends on how it is added:
includeinserts the module into the class’s ancestor chain (for example, its methods become methods on instances of the class).extendinserts the module into the object’s singleton class (its methods are available directly on the extended object).
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents