How do I use define_method to create class methods?

前端 未结 6 1124
孤城傲影
孤城傲影 2020-11-30 20:34

This is useful if you are trying to create class methods metaprogramatically:

def self.create_methods(method_name)
    # To create instance methods:
    defi         


        
6条回答
  •  死守一世寂寞
    2020-11-30 21:11

    To be used in Rails if you want to define class methods dynamically from concern:

    module Concerns::Testable
      extend ActiveSupport::Concern
    
      included do 
        singleton_class.instance_eval do
          define_method(:test) do
            puts 'test'
          end
        end
      end
    end
    

提交回复
热议问题