2014年1月23日木曜日

Rubyのmoduleのmix-in

moduleをincludeしてclassにmix-inした際には、module内のインスタンスメソッドをそのclassのインスタンスメソッドとして呼べるようになる。

module Asdf
  def qwer; "qwer"; end
end

class Zxcv
  include Asdf
  def sdfg; qwer; end #=> "qwer"
end

class Xcvb
  def sdfg; Asdf.qwer; end #=> NoMethodError: undefined method `qwer' for Asdf:module
end
それだけ。ただそれだけ。

0 件のコメント:

コメントを投稿