新手问题 Ruby class mixin 一个模块能否有些函数成为类函数,有些函数成为实例方法

lilijreey · 2016年10月28日 · 最后由 rikiwong 回复于 2016年10月28日 · 2075 次阅读

我需要 mixin 一个模块,但是模块中的一些函数成为类函数,一些函数成为实例方法. 请问如何做到

Ruby Pattern: Extend through Include

http://www.dan-manges.com/blog/27

module Module
  module ClassMethods

  end

  module InstanceMethods

  end

  def self.included(receiver)
    receiver.extend         ClassMethods
    receiver.send :include, InstanceMethods
  end
end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号