我正在製作一個框架,根據預定義的XML文件創建對象。 例如,如果在XML文件中會出現以下情況:使用元編程設計Ruby應用程序
<type name="man">
<property name="name" type="string">
<property name="height" type="int">
<property name="age" type="int">
<property name="profession" type="string" value="unemployed">
</type>
在Ruby中,這應該允許您創建一個對象如下:
man = Man.new('John', 188, 30)
注:對於字段,其中「值'是在xml中定義的,在初始化方法中不應該接受 值,而應該是由類本身設置的 作爲默認值。
任何推薦的實現? 我目前在觀看Dave Thomas關於元編程的屏幕錄像, 所以這看起來很合適,但任何建議,將不勝感激!
如果我添加的參數一定的順序,我可以按照相同的順序提取它們嗎?我嘗試了'instance_variables',但是按照相反的順序對它們進行了git。訂單是否有保證? – 2009-06-28 19:55:30