Ruby: Is it possible to define a class method in a module?

前端 未结 5 1038
萌比男神i
萌比男神i 2020-12-24 00:19

Say there are three classes: A, B & C. I want each class to have a class method, say self.foo, that has exactly the s

5条回答
  •  情书的邮戳
    2020-12-24 00:48

    Rails 3 introduced a module named ActiveSupport::Concern which has the goal of simplifying the syntax of modules.

    module Foo
      extend ActiveSupport::Concern
    
      module ClassMethods
        def some_method
          # stuff
        end
      end
    end
    

    It allowed us to save a few lines of "boilerplate" code in the module.

提交回复
热议问题