I've been trying to embed Ruby in a C# program that I'm writing. I can execute scripts, but I'm having issues with interop. I can't seem to extract variables from Ruby.
Let us consider the following:
String before = "hello world";
String after = "changed";
ScriptRuntime mRuntime = Ruby.CreateRuntime();
ScriptEngine mEngine = mRuntime.GetEngine("ruby");
ScriptScope scope = mEngine.CreateScope();
String code = "p msg\nmsg = '" + after + "'";
ScriptSource script = mEngine.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
scope.SetVariable("msg", before);
script.Execute(scope);
String result = scope.GetVariable("msg");
(Originally this was implemented using TryGetVariable)
In my unit tests I set an assert on result == "changed" but it carries it's default value. I've also tried creating a variable in Ruby (local and $global) and reading it out but it seems to never exist.
I'm a bit stuck, any help would be apreciated.
String before = "hello world";
String after = "changed";
ScriptRuntime mRuntime = Ruby.CreateRuntime();
ScriptEngine mEngine = mRuntime.GetEngine("ruby");
ScriptScope scope = mEngine.CreateScope();
String code = "self.msg = '" + after + "'.to_clr_string";
ScriptSource script = mEngine.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
scope.SetVariable("msg", before);
script.Execute(scope);
String result = scope.GetVariable("msg");
This works for me.