A simple mixin with extend
suggest changeA mixin is just a module that can be added (mixed in) to a class. One way to do it is with extend. The extend method adds the mixin’s methods as class methods.
module SomeMixin
def foo
puts "foo!"
end
end
class Bar
extend SomeMixin
def baz
puts "baz!"
end
end
b = Bar.new
b.baz # => "baz!"
b.foo # NoMethodError, as the method was NOT added to the instance
Bar.foo # => "foo!"
# works only on the class itself
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents