一个简单的 mixin 扩展

mixin 只是一个可以添加(混入)到类的模块。一种方法是使用 extend 方法。extend 方法将 mixin 的方法添加为类方法。

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