开发者

why do we need ClassMethods and InstanceMethods?

开发者 https://www.devze.com 2023-04-05 12:39 出处:网络
I read the API for ActiveSupport::Concern. There a开发者_如何转开发re ClassMethods and InstanceMethods, we can put class methods in ClassMethods.

I read the API for ActiveSupport::Concern. There a开发者_如何转开发re ClassMethods and InstanceMethods, we can put class methods in ClassMethods.

But the M's host can use the methods defined in M, can't it? Why can't I just write:

module M
  def self.x
  end

  def y
  end
end

rather than:

module M
  module ClassMethods
    def x
    end
  end
  module InstanceMethods
    def y
    end
  end
end


You might be interested in Yehuda's take on this pattern. I think the reason for some of it is historical, since they're not really needed unless you're doing manually what Ruby will do automatically through include and extend.


Dependencies are taken care of. See the example provided here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号