下面的代码为给定数组设置类方法:
class Testing
V4_RELATIONSHIP_TYPES=[1=>2,3=>4]
V4_RELATIONSHIP_TYPES.keys.each do |key|
self.class.send(:define_method, "get_#{key}_type".downcase) do
return GuidInfo.get_or_new(PARAMS, V4_RELATIONSHIP_TYPES[key])
end
end
end
#so i can call Testing.get_1_key()
问题是:如何为实例获取相同的方法集?
答案 0 :(得分:4)
self.send(:method, value)
答案 1 :(得分:2)
class Testing
V4_RELATIONSHIP_TYPES = { 1 => 2, 3 => 4 }
V4_RELATIONSHIP_TYPES.each do |key, value|
define_method("get_#{key}_type".downcase) do
return GuidInfo.get_or_new(PARAMS, value)
end
end
end
# Now you can call Testing.new.get_1_key