3

I have an IronRuby script that modifies the value of a variable set via the ScriptScope.

I'd like to retrieve the value of the variable after it's been modified, but I get the old value.

This is the code I have:

var engine = Ruby.CreateEngine();
var scope = engine.Runtime.CreateScope();

scope.SetVariable("y", 11);

var source = engine.CreateScriptSourceFromString("y=33; puts y;", SourceCodeKind.Statements);
source.Execute(scope);

The above code executes and outputs 33 to the console, which is OK.

However, I tried adding the following line after the above code:

Console.WriteLine(scope.GetVariable("y"));

and it outputs 11, which is the original value.

Is there a way to get the new variable value?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137

2 Answers2

1

With IronPython, to retrieve a value, I use a proxy object like this.

public class ScriptProxy
{
    private int _result;

    public int Result
    {
        get { return this._result; }
        set { this._result = value; }
    }
}

And I call SetVariable to pass an instance of the ScriptObject :

ScriptEngine pyEngine = Python.CreateEngine();
ScriptScope pyScope = pyEngine.CreateScope();

ScriptProxy proxy = new ScriptProxy();
pyScope.SetVariable("proxy", proxy);

In your script you can set the result :

proxy.Result=33;
Hyralex
  • 990
  • 1
  • 8
  • 25
  • I've tested your solution and it works. This must be because of the difference between reference and value types, although I would have expected primitive types to work in this scenario as well. The workaround you presented is acceptable in my case, so I'll accept it as the answer. Thanks and +1! – Cristian Lupascu Jun 05 '12 at 17:40
  • Indeed, it's because the object is pass in reference. But I don't know if we can pass a datatype base in ref! – Hyralex Jun 06 '12 at 10:01
0

Setvariable is to change inputs

e.g.

if you had y = y + 1

Before the DLR you would have made your script 'y = 11' followed by 'y = y + 1' and if you then wanted to run it again for y = 15, you would have built a new script initialising y to 15 and then it would have been interpreted again.

What's happening is y = 33 is getting replaced by y = 11, setvariable is so you can use the same compiled script for different inputs

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • "y = 33 is getting replaced by y = 11" - that doesn't seem to happen, as the ruby engine outputs `33` when I run the script. Also, the `y = 11` comes before `y = 33`. I found this out after changing the script to `puts y; y=33; puts y;`. This outputs `11` and afterwards `33`, as expected. – Cristian Lupascu Jun 03 '12 at 20:45
  • if y is still coming back as 11 even though it "should" be 33, this suggests something else is happening, though what I'm not exactly sure. We use IronPython, but the principles are similar, I'll have a rummage about, haven't beenin there in while see if I can spot something. – Tony Hopkinson Jun 03 '12 at 23:27