# Setting global variables in embedded ironruby interpreter

**URL:** <https://www.ruby-forum.com/t/setting-global-variables-in-embedded-ironruby-interpreter/159849>\
**Category:** IronRuby\
**Created:** [February 9, 2009, 12:38pm UTC](https://www.ruby-forum.com/t/setting-global-variables-in-embedded-ironruby-interpreter/159849 "2009-02-09T12:38:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [February 9, 2009, 12:38pm UTC](https://www.ruby-forum.com/t/setting-global-variables-in-embedded-ironruby-interpreter/159849/1 "2009-02-09T12:38:54Z")

</div>

Hello,

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

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [February 9, 2009, 1:24pm UTC](https://www.ruby-forum.com/t/setting-global-variables-in-embedded-ironruby-interpreter/159849/2 "2009-02-09T13:24:56Z")

</div>

On Mon, Feb 9, 2009 at 12:12 PM, Meinrad R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> 

Oh, i just found out that ScriptScope.GetVariable is throwing an  
exception  
…

* * *

ArgumentNullException: “Value cannot be null.  
Parameter name: field”

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [February 9, 2009, 3:42pm UTC](https://www.ruby-forum.com/t/setting-global-variables-in-embedded-ironruby-interpreter/159849/3 "2009-02-09T15:42:52Z")

</div>

On Mon, Feb 9, 2009 at 1:05 PM, Meinrad R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > assigning it to a global via engine.Execute(“$a=a”, scope).  
> > Parameter name: field"
> 
> * * *
> 
> Microsoft.Scripting.Utils.ContractUtils.RequiresNotNull(Object value,  
> String paramName)  
> Microsoft.Scripting.SymbolTable.StringToId(String field)  
> Microsoft.Scripting.Hosting.ScriptScope.GetVariable(String name)

sorry forgot to say, that I made sure, that the “name” parameter is  
_not_  
null.  
I am not yet deep enough into the sources to fix things like these  
myself.

– henon

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [February 9, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/setting-global-variables-in-embedded-ironruby-interpreter/159849/4 "2009-02-09T15:54:05Z")

</div>

On Mon, Feb 9, 2009 at 1:11 PM, Meinrad R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > >
> 
> sorry forgot to say, that I made sure, that the “name” parameter is _not_  
> null.  
> I am not yet deep enough into the sources to fix things like these myself.
> 
> – henon

ok, I was too fast on this one. actually it was null because a  
ruby\_string  
casted to c# string is null. It is pretty dangerous to assume a value  
presented as “a” by the debugger to be a System.String when that value  
comes  
out from ruby 😉

so the problem is not the exception but rather that GetVariable(“a”)  
does  
not find existing local variables which can be evaluated by  
Execute(“a”).  
This time I made sure I made no mistake.

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [February 9, 2009, 7:09pm UTC](https://www.ruby-forum.com/t/setting-global-variables-in-embedded-ironruby-interpreter/159849/5 "2009-02-09T19:09:37Z")

</div>

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(“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);

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] 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

Hello,

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
