[Ironruby-core] setting global variables in embedded ironruby interpreter

Tomas Matousek Tomas.Matousek at microsoft.com
Mon Feb 9 12:28:19 EST 2009

You can find many hosting examples in HostingTests.cs.

As for global and top-level local variables:

-          Variables in ScriptRuntimeRuntime.Globals are mapped to global constants (ie. constants on Object).

-          Variables in local ScriptScope are not mapped to Ruby top-level local variables. They are looked up via method_missing on a top-level singleton object. Ruby top-level program executes in a context where "self" is a singleton of Object. IronRuby defines method_missing on this singleton. If a method name ends with "=" it writes to the scope, otherwise it reads from the scope.

ScriptScope scope = Engine.CreateScope();
scope.SetVariable("x", 1);
scope.SetVariable("y", 2);
Engine.Execute("self.z = x + y", scope);
int result = scope.GetVariable<int>("result");
Assert(result == 3);

Ruby global variables don't have any mapping to DLR scopes. We don't have any well designed API for them yet, you can do this for now:

Ruby.GetExecutionContext(Engine).DefineGlobalVariable("foo", 123);


From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Meinrad Recheis
Sent: Monday, February 09, 2009 3:13 AM
To: ironruby-core
Subject: [Ironruby-core] setting global variables in embedded ironruby interpreter


I am very pleased that I got everything working and found iron ruby in a quite usable state for me. Congratulations.

Question: How do you set a global variable from C#? I found a workaround via setting a local variable scope.SetVariable("a", obj) in the scope and assigning it to a global via engine.Execute("$a=a", scope).
The Runtime.Globals.GetVariable and SetVariable don't seem to get / set the ruby globals.
Please clarify.

BTW: as for local variables: scope.GetVariableNames() does return an empty list. Again a workaround is Execute("local_variables", scope).
-- henon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20090209/97e814a6/attachment-0001.html>

More information about the Ironruby-core mailing list