Define a class method in a module

后端 未结 2 767
我寻月下人不归
我寻月下人不归 2021-01-26 19:17

I mean, I want to create a class method in my module that will be used by the class who includes the module. They are in separated files.

By far I have something like th

2条回答
  •  面向向阳花
    2021-01-26 20:10

    Following Ricardo's answer, consider an idiom common among Ruby programmers - enclose your module's class methods into inner module, called ClassMethods(that's a mouthful, I know), and use and Module#included hook to extend base class with ClassMethods module.

    More information here: http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/

提交回复
热议问题