gpt4 book ai didi

ruby - 如何使用 define_method 创建类方法?

转载 作者:数据小太阳 更新时间:2023-10-29 06:17:57 24 4
gpt4 key购买 nike

如果您尝试以元编程方式创建类方法,这将很有用:

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

关于ruby - 如何使用 define_method 创建类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/752717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com