我在试着理解这个函数。self.class_eval << DEF ... DEF
我可以看到的是一个属性和类型传递给opal
()方法。
然后type_name
只要type
是Symbol
或String
需要从type
其值。否则,在type
上调用name
方法。我想象name
方法类似于class
方法来获得type
参数的类。
后self.class_eval
我有点失落,但我的猜测是这是定义可能是一个代码块被添加到self
引用的类。
这是如何工作我不确定。
如果有人能解释self.class_eval << DEF
后发生了什么,我们将不胜感激。
def opal(attr, type)
self.ds "#{attr}_id"
type_name = (type.is_a?(Symbol) || type.is_a?(String)) ? type : type.name
self.class_eval <<DEF
def #{attr}
if defined?(@#{attr})
@#{attr}
else
@#{attr} = if self.#{attr}_id
#{type_name}.get(self.#{attr}_id)
else
nil
end
end
end
def #{attr}=(value)
self.#{attr}_id = value.key
@#{attr} = value
end
DEF
end
为什么这次被投票?它与编程有关,这个问题似乎并不清楚。 – 2009-06-10 05:00:55