How do I add a class instance variable, the data for it and a attr_reader at runtime?
class Module
def additional_data member, data
self.class.send(:define_method, member) {
p "Added method #{member} to #{name}"
}
end
end
For example, given this class
class Test
additional_data :status, 55
end
开发者_开发知识库So that now I can call:
p Test.status # => prints 55
How about this?
class Object
def self.additional_data(name, value)
ivar_name = "@#{name}"
instance_variable_set(ivar_name, value)
self.class.send(:define_method, name) do
instance_variable_get(ivar_name)
end
self.class.send(:define_method, "#{name}=") do |new_value|
instance_variable_set(ivar_name, new_value)
end
end
end
class Foo
additional_data :bar, 'baz'
end
puts Foo.bar # => 'baz'
Foo.bar = 'quux'
puts Foo.bar # => 'quux'
It's pretty self-explanatory, but let me know if you have any questions.
Module#class_eval
is what you want:
def add_status(cls)
cls.class_eval do
attr_reader :status
end
end
add_status(Test)
精彩评论