我正在尝试编写一个具有一些元编程功能的 ruby 模块,但我有点困惑。
module MetaModule
extend ActiveSupport::Concern
module ClassMethods
def my_method(attribute)
# Define an attr_accessor for the original class
attr_accessor :test_accessor
# This is clearly wrong, but I don't know what is correct
self.test_accessor ||= []
self.test_accessor << attribute
end
end
end
class MyClass
include MetaModule
my_method :name
my_method :age
my_method :city
end
我想要的输出是:MyClass.new.test_accessor => [:name, :age, :city]