Programing

클래스 메소드를 생성하기 위해 define_method를 어떻게 사용합니까?

crosscheck 2020. 8. 13. 07:51
반응형

클래스 메소드를 생성하기 위해 define_method를 어떻게 사용합니까?


메타 프로그래밍 방식으로 클래스 메서드를 생성하려는 경우 유용합니다.

def self.create_methods(method_name)
    # To create instance methods:
    define_method method_name do
      ...
    end

    # To create class methods that refer to the args on create_methods:
    ???
end

따라야 할 내 대답 ...


Ruby 1.9에서는 다음과 같이 할 수 있습니다.

class A
  define_singleton_method :loudly do |message|
    puts message.upcase
  end
end

A.loudly "my message"

# >> MY MESSAGE

저는 send를 사용하여 define_method를 호출하는 것을 선호하며 메타 클래스에 액세스하기 위해 메타 클래스 메서드를 만들고 싶습니다.

class Object
  def metaclass
    class << self
      self
    end
  end
end

class MyClass
  # Defines MyClass.my_method
  self.metaclass.send(:define_method, :my_method) do
    ...
  end
end

이것은 Ruby 1.8+에서 가장 간단한 방법입니다.

class A
  class << self
    def method_name
      ...
    end
  end
end

출처 : Jay and Why , 누가 더 예쁘게 만드는 방법도 제공합니다.

self.create_class_method(method_name)
  (class << self; self; end).instance_eval do
    define_method method_name do
      ...
    end
  end
end

업데이트 : 아래 VR의 기여에서; 여전히 독립형 인 더 간결한 방법 (이 방법으로 하나의 방법 만 정의하는 한) :

self.create_class_method(method_name)
  (class << self; self; end).send(:define_method, method_name) do
    ...
  end
end

그러나 send ()를 사용하여 define_method ()와 같은 개인 메서드에 액세스하는 것은 반드시 좋은 생각은 아닙니다 (내 이해는 Ruby 1.9에서 사라질 것입니다).


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

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

참고URL : https://stackoverflow.com/questions/752717/how-do-i-use-define-method-to-create-class-methods

반응형