How do I use define_method to create class methods?

前端 未结 6 1129
孤城傲影
孤城傲影 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:22

    You could also do something like this without relying on define_method:

    A.class_eval do
      def self.class_method_name(param)
        puts param
      end
    end
    
    A.class_method_name("hello") # outputs "hello" and returns nil
    

提交回复
热议问题