Question

I have this line of IronPython code:

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

Traits is defined as such:

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

I would like to translate the IronPython code into IronRuby, but I'm having trouble finding the correct syntax.

Was it helpful?

Solution

In Ruby (and IronRuby), variables must begin with a lowercase letter. Therefore, just change the Traits variable to traits to make your code works:

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

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

(this code works, I checked).

By the way, creating a variable that starts with a capital letter makes it a constant (that's how Ruby works) and adding a constant to the scope is done in a slightly different way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top