特異メソッドでincludeする方法

何個か方法あるので、少しづつ紹介します。

クラス内でincludeする

module Foo
  def hello
    'hello'
  end
end

class Bar
  class << self
    include Foo
  end
end

Bar.hello
=> "hello"

特定のオブジェクトだけincludeする

x = Bar.new
class << x
  include Foo
end

x.hello
=> "hello"

Object#extend

これが一番一般的ですね。

y = Bar.new
y.extend Foo
y.hello
=> "hello"

方法としては、この3つあります。

意外にextendの用法を忘れてしまう・・・orz