2010-07-25 28 views
0

我這行代碼的IronPython:字典查找如何在IronRuby中工作?

Traits['Strength'].Score + Traits['Dexterity'].Score 

性狀的定義是這樣的:

Dim m_Traits As New Dictionary(Of String, Trait) 
scope.SetVariable("Traits", m_Traits) 

我想翻譯的IronPython代碼到IronRuby的,但我無法找到正確的句法。

回答

1

在Ruby(和IronRuby)中,變量必須以小寫字母開頭。因此,就在Traits變量更改爲traits,使您的代碼工作:

var engine = IronRuby.Ruby.CreateEngine(); 
var scope = engine.CreateScope(); 
scope.SetVariable("traits", traits); 

dynamic result = engine.Execute("traits['Strength'].Score + traits['Dexterity'].Score", scope); 

(此代碼的工作,我選中)。

順便說一下,創建一個以大寫字母開頭的變量使其成爲一個常量(這就是Ruby的工作方式)並向範圍添加常量的方式稍有不同。

+0

那肯定會解釋我看到的奇怪的錯誤信息。謝謝。 – 2010-07-26 14:37:13