# Passing a .NET object to the RubyEngine

**URL:** <https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760>\
**Category:** IronRuby\
**Created:** [September 28, 2007, 8:46am UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760 "2007-09-28T08:46:56Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 28, 2007, 8:46am UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/1 "2007-09-28T08:46:56Z")

</div>

Hi All, I posted some code for calling IronRuby code I found in the  
mailing  
list onto the wiki.

[http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp](http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp)

RubyEngine re = RubyEngine.CurrentEngine;

string script = “s = ‘let us get started’” + Environment.NewLine

```
+ "i = s.length");

```

re.ExecuteCommand(script);

The only thing is, I don’t know how to pass a .NET object to the context  
of  
the RubyEngine so it can be called from within the script. I’d like to  
update the example with that.

Any help?

Phil

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 28, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/2 "2007-09-28T15:39:16Z")

</div>

Here’s a quick example:

RubyEngine re = RubyEngine.CurrentEngine;  
IScriptModule mod =  
ScriptDomainManager.CurrentManager.Host.DefaultModule

string script = “puts ‘Ruby and ’ + var.to\_s + ’ together at last’”

mod.SetVariable(“var”, “.NET”)

re.ExecuteCommand(script, mod);  
A couple things to note:

1. var.to\_s is necessary because I don’t think IR is coercing CLR  
Strings to  
Ruby Strings yet.
2. I’m not sure why you need to pass in the default module to  
ExecuteCommand, but I wasn’t able to evaluate the variable in Ruby  
otherwise.

-Eric

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 28, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/3 "2007-09-28T15:40:26Z")

</div>

Whoops … missing some semicolons there. Can you tell I spend most of  
my  
day in [VB.NET](http://VB.NET)?

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 28, 2007, 6:13pm UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/4 "2007-09-28T18:13:33Z")

</div>

Eric N. wrote:

> A couple things to note:
> 
> 1. var.to\_s is necessary because I don’t think IR is coercing CLR  
> Strings to Ruby Strings yet.
> 2. I’m not sure why you need to pass in the default module to  
> ExecuteCommand, but I wasn’t able to evaluate the variable in Ruby  
> otherwise.

How can this work? In order for var to be available in the script, it  
would have to be added into the toplevel scope. This would either mean  
that var is now available in all toplevel scopes across the system  
(incompatible with Ruby) or that it would only be available in the  
current thread’s scope above toplevel (which would prevent seeing it in  
other threads. I’m a little confused. In JRuby we opted to not allow  
external variable binding into the eventual execution scope, because it  
seemed to violate that the scope should be created fresh for each script  
execution.

- Charlie

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 28, 2007, 7:29pm UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/5 "2007-09-28T19:29:15Z")

</div>

Someone at MS can probably answer this better than I, but I’m guessing  
that  
passing in the module explicitly to the ExecuteCommand statement is what  
makes this work. If you leave off that optional parameter the variable  
isn’t directly accessible.

You can always create a new module fairly easily and pass that in as  
well if  
you’d rather.

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 28, 2007, 7:53pm UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/6 "2007-09-28T19:53:18Z")

</div>

Eric N. wrote:

> Someone at MS can probably answer this better than I, but I’m guessing  
> that passing in the module explicitly to the ExecuteCommand statement is  
> what makes this work. If you leave off that optional parameter the  
> variable isn’t directly accessible.
> 
> You can always create a new module fairly easily and pass that in as  
> well if you’d rather.

Local variables aren’t stored in modules though; unless this is adding  
an attribute, which would seem really wrong.

- Charlie

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 28, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/7 "2007-09-28T20:09:41Z")

</div>

I believe there are two types of “Modules” at play here. I think a DLR  
“Module” is something more like a scope than the Ruby “Module”.

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 28, 2007, 7:36pm UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/8 "2007-09-28T19:36:17Z")

</div>

Nice! I’ll update the wiki. This worked for me.

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Eric N.  
Sent: Friday, September 28, 2007 6:38 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

Here’s a quick example:

RubyEngine re = RubyEngine.CurrentEngine;  
IScriptModule mod =  
ScriptDomainManager.CurrentManager.Host.DefaultModule

string script = “puts ‘Ruby and ’ + var.to\_s + ’ together at last’”

mod.SetVariable(“var”, “.NET”)

re.ExecuteCommand(script, mod);

A couple things to note:

1. var.to\_s is necessary because I don’t think IR is coercing CLR  
Strings to  
Ruby Strings yet.
2. I’m not sure why you need to pass in the default module to  
ExecuteCommand, but I wasn’t able to evaluate the variable in Ruby  
otherwise.

-Eric

On 9/28/07, Phil H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Hi All, I posted some code for calling IronRuby code I found in the  
mailing  
list onto the wiki.

[http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp](http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp)

RubyEngine re = RubyEngine.CurrentEngine;

string script = “s = ‘let us get started’” + Environment.NewLine

```
+ "i = s.length");

```

re.ExecuteCommand(script);

The only thing is, I don’t know how to pass a .NET object to the context  
of  
the RubyEngine so it can be called from within the script. I’d like to  
update the example with that.

Any help?

Phil

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 29, 2007, 9:58am UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/9 "2007-09-29T09:58:04Z")

</div>

Well, technically between Ruby and [VB.Net](http://VB.Net), but yeah, absolutely, works  
quite  
well too.

The string above is actually a “real” object the way Ruby sees it, so  
it’s  
basically the same. I’ll put a quick example together if no one beats  
me to  
it.

-Eric

On 9/28/07, Stefan HÃ¼ttenrauch [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 29, 2007, 10:10am UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/10 "2007-09-29T10:10:20Z")

</div>

That’s what I was thinking too. Here’s what I put together:

class Program  
{  
static void Main(string[] args)  
{  
RubyEngine re = RubyEngine.CurrentEngine;  
IScriptModule scriptModule =  
ScriptDomainManager.CurrentManager.Host.DefaultModule;  
string script = “require ‘CSIronRuby’; puts ‘Hello, ’ +  
var.GetName.to\_s + ’ World!’”;  
MyName name = new MyName();  
scriptModule.SetVariable(“var”, name);  
re.ExecuteCommand(script, scriptModule);  
Console.ReadLine();  
}  
}

public class MyName  
{  
public string Name = “Cory”;

```
public string GetName()
{
     return Name;
}

```

}

For some reason, I was having difficulty getting it to output just Name

- I just didn’t want to tear into it.

Cory

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Stefan  
HÃ¼ttenrauch  
Sent: Friday, September 28, 2007 10:49 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

Nice stuff Eric,

Have you already passed real objects back and forth between Ruby and  
C#?? That would be the real cool stuff. I will hack a little tomorrow I  
guess, but if you have any tips already, that would be great.

Thanks  
Stefan

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Eric N.  
Sent: Freitag, 28. September 2007 15:40  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

Whoops … missing some semicolons there. Can you tell I spend most of  
my day in [VB.NET](http://VB.NET)[http://VB.NET](http://VB.NET)?  
On 9/28/07, Eric N. \<  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Here’s a quick example:

RubyEngine re = RubyEngine.CurrentEngine;  
IScriptModule mod =  
ScriptDomainManager.CurrentManager.Host.DefaultModule

string script = “puts ‘Ruby and ’ + var.to\_s + ’ together at last’”

mod.SetVariable(“var”, “.NET”)

re.ExecuteCommand(script, mod);  
A couple things to note:

1. var.to\_s is necessary because I don’t think IR is coercing CLR  
Strings to Ruby Strings yet.
2. I’m not sure why you need to pass in the default module to  
ExecuteCommand, but I wasn’t able to evaluate the variable in Ruby  
otherwise.

-Eric  
On 9/28/07, Phil H. \<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\>  
wrote:

Hi All, I posted some code for calling IronRuby code I found in the  
mailing list onto the wiki.

[http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp](http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp)

RubyEngine re = RubyEngine.CurrentEngine;

string script = “s = ‘let us get started’” + Environment.NewLine

```
+ "i = s.length");

```

re.ExecuteCommand(script);

The only thing is, I don’t know how to pass a .NET object to the context  
of the RubyEngine so it can be called from within the script. I’d like  
to update the example with that.

Any help?

Phil

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 29, 2007, 12:36pm UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/11 "2007-09-29T12:36:58Z")

</div>

Sorry, ignore the require line in the script - that was cruft left over  
from something else I was doing and isn’t needed.

Cory

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Cory F.  
Sent: Friday, September 28, 2007 11:02 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

That’s what I was thinking too. Here’s what I put together:

class Program  
{  
static void Main(string[] args)  
{  
RubyEngine re = RubyEngine.CurrentEngine;  
IScriptModule scriptModule =  
ScriptDomainManager.CurrentManager.Host.DefaultModule;  
string script = “require ‘CSIronRuby’; puts ‘Hello, ’ +  
var.GetName.to\_s + ’ World!’”;  
MyName name = new MyName();  
scriptModule.SetVariable(“var”, name);  
re.ExecuteCommand(script, scriptModule);  
Console.ReadLine();  
}  
}

public class MyName  
{  
public string Name = “Cory”;

```
public string GetName()
{
     return Name;
}

```

}

For some reason, I was having difficulty getting it to output just Name

- I just didn’t want to tear into it.

Cory

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Stefan  
HÃ¼ttenrauch  
Sent: Friday, September 28, 2007 10:49 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

Nice stuff Eric,

Have you already passed real objects back and forth between Ruby and  
C#?? That would be the real cool stuff. I will hack a little tomorrow I  
guess, but if you have any tips already, that would be great.

Thanks  
Stefan

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Eric N.  
Sent: Freitag, 28. September 2007 15:40  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

Whoops … missing some semicolons there. Can you tell I spend most of  
my day in [VB.NET](http://VB.NET)[http://VB.NET](http://VB.NET)?  
On 9/28/07, Eric N. \<  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Here’s a quick example:

RubyEngine re = RubyEngine.CurrentEngine;  
IScriptModule mod =  
ScriptDomainManager.CurrentManager.Host.DefaultModule

string script = “puts ‘Ruby and ’ + var.to\_s + ’ together at last’”

mod.SetVariable(“var”, “.NET”)

re.ExecuteCommand(script, mod);  
A couple things to note:

1. var.to\_s is necessary because I don’t think IR is coercing CLR  
Strings to Ruby Strings yet.
2. I’m not sure why you need to pass in the default module to  
ExecuteCommand, but I wasn’t able to evaluate the variable in Ruby  
otherwise.

-Eric  
On 9/28/07, Phil H. \<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\>  
wrote:

Hi All, I posted some code for calling IronRuby code I found in the  
mailing list onto the wiki.

[http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp](http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp)

RubyEngine re = RubyEngine.CurrentEngine;

string script = “s = ‘let us get started’” + Environment.NewLine

```
+ "i = s.length");

```

re.ExecuteCommand(script);

The only thing is, I don’t know how to pass a .NET object to the context  
of the RubyEngine so it can be called from within the script. I’d like  
to update the example with that.

Any help?

Phil

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 29, 2007, 2:48pm UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/12 "2007-09-29T14:48:28Z")

</div>

This is part of the hosting API we provide for host applications so that  
they can pass objects to and from a dynamic language (be it Ruby or  
Python), execute some code within some scope etc.

This API gradually undergoes major changes and it isn’t fully compatible  
with Ruby’s semantics today. We already have some design on “paper”  
(needs to be polished/reviewed still) that makes it right. It’s  
implementation will take some time though. The code bellow should kind  
of work (there is a little tweak to the variable binding in the Ruby  
compiler to allow it) until we provide some better way. Good news is  
that we found a way how to do the integration right for Ruby, so all  
features listed above (passing objects there and back, executing code,  
etc) will be available in future.

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Cory F.  
Sent: Friday, September 28, 2007 8:02 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

That’s what I was thinking too. Here’s what I put together:

class Program  
{  
static void Main(string[] args)  
{  
RubyEngine re = RubyEngine.CurrentEngine;  
IScriptModule scriptModule =  
ScriptDomainManager.CurrentManager.Host.DefaultModule;  
string script = “require ‘CSIronRuby’; puts ‘Hello, ’ +  
var.GetName.to\_s + ’ World!’”;  
MyName name = new MyName();  
scriptModule.SetVariable(“var”, name);  
re.ExecuteCommand(script, scriptModule);  
Console.ReadLine();  
}  
}

public class MyName  
{  
public string Name = “Cory”;

```
public string GetName()
{
     return Name;
}

```

}

For some reason, I was having difficulty getting it to output just Name

- I just didn’t want to tear into it.

Cory

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Stefan  
HÃ¼ttenrauch  
Sent: Friday, September 28, 2007 10:49 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

Nice stuff Eric,

Have you already passed real objects back and forth between Ruby and  
C#?? That would be the real cool stuff. I will hack a little tomorrow I  
guess, but if you have any tips already, that would be great.

Thanks  
Stefan

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Eric N.  
Sent: Freitag, 28. September 2007 15:40  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

Whoops … missing some semicolons there. Can you tell I spend most of  
my day in [VB.NET](http://VB.NET)[http://VB.NET](http://VB.NET)?  
On 9/28/07, Eric N. \<  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Here’s a quick example:

RubyEngine re = RubyEngine.CurrentEngine;  
IScriptModule mod =  
ScriptDomainManager.CurrentManager.Host.DefaultModule

string script = “puts ‘Ruby and ’ + var.to\_s + ’ together at last’”

mod.SetVariable(“var”, “.NET”)

re.ExecuteCommand(script, mod);  
A couple things to note:

1. var.to\_s is necessary because I don’t think IR is coercing CLR  
Strings to Ruby Strings yet.
2. I’m not sure why you need to pass in the default module to  
ExecuteCommand, but I wasn’t able to evaluate the variable in Ruby  
otherwise.

-Eric  
On 9/28/07, Phil H. \<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\>  
wrote:

Hi All, I posted some code for calling IronRuby code I found in the  
mailing list onto the wiki.

[http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp](http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp)

RubyEngine re = RubyEngine.CurrentEngine;

string script = “s = ‘let us get started’” + Environment.NewLine

```
+ "i = s.length");

```

re.ExecuteCommand(script);

The only thing is, I don’t know how to pass a .NET object to the context  
of the RubyEngine so it can be called from within the script. I’d like  
to update the example with that.

Any help?

Phil

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 30, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/13 "2007-09-30T20:25:20Z")

</div>

BTW, Charles,

congrats on the release of JRuby.

Josh Nursing

Charles Oliver N. wrote:

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [September 29, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/14 "2007-09-29T17:22:22Z")

</div>

I updated the demo code in the wiki based on the code snippet here.

[http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp](http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp)

IÂ’ll try and make sure it stays up to date. J

Phil

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Tomas M.  
Sent: Friday, September 28, 2007 12:46 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

This is part of the hosting API we provide for host applications so that  
they can pass objects to and from a dynamic language (be it Ruby or  
Python),  
execute some code within some scope etc.

This API gradually undergoes major changes and it isnÂ’t fully compatible  
with RubyÂ’s semantics today. We already have some design on Â“paperÂ”  
(needs  
to be polished/reviewed still) that makes it right. ItÂ’s implementation  
will  
take some time though. The code bellow should kind of work (there is a  
little tweak to the variable binding in the Ruby compiler to allow it)  
until  
we provide some better way. Good news is that we found a way how to do  
the  
integration right for Ruby, so all features listed above (passing  
objects  
there and back, executing code, etc) will be available in future.

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Cory F.  
Sent: Friday, September 28, 2007 8:02 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

ThatÂ’s what I was thinking too. HereÂ’s what I put together:

class Program

{

```
static void Main(string[] args)

{

     RubyEngine re = RubyEngine.CurrentEngine;

     IScriptModule scriptModule =

```

ScriptDomainManager.CurrentManager.Host.DefaultModule;

```
     string script = "require 'CSIronRuby'; puts 'Hello, ' +

```

var.GetName.to\_s + ’ World!'";

```
     MyName name = new MyName();

     scriptModule.SetVariable("var", name);

     re.ExecuteCommand(script, scriptModule);

     Console.ReadLine();

}

```

}

public class MyName

{

```
public string Name = "Cory";

public string GetName()

{

     return Name;

}

```

}

For some reason, I was having difficulty getting it to output just Name  
Â– I  
just didnÂ’t want to tear into it.

Cory

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Stefan  
HÃ¼ttenrauch  
Sent: Friday, September 28, 2007 10:49 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

Nice stuff Eric,

Have you already passed real objects back and forth between Ruby and  
C#??  
That would be the real cool stuff. I will hack a little tomorrow I  
guess,  
but if you have any tips already, that would be great.

Thanks

Stefan

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Eric N.  
Sent: Freitag, 28. September 2007 15:40  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Passing a .NET object to the RubyEngine…

Whoops … missing some semicolons there. Can you tell I spend most of  
my  
day in [VB.NET](http://VB.NET)?

On 9/28/07, Eric N. \< removed\_email\_address@domain.invalid  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \> wrote:

Here’s a quick example:

RubyEngine re = RubyEngine.CurrentEngine;  
IScriptModule mod =  
ScriptDomainManager.CurrentManager.Host.DefaultModule

string script = “puts ‘Ruby and ’ + var.to\_s + ’ together at last’”

mod.SetVariable(“var”, “.NET”)

re.ExecuteCommand(script, mod);

A couple things to note:

1. var.to\_s is necessary because I don’t think IR is coercing CLR  
Strings to  
Ruby Strings yet.
2. I’m not sure why you need to pass in the default module to  
ExecuteCommand, but I wasn’t able to evaluate the variable in Ruby  
otherwise.

-Eric

On 9/28/07, Phil H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Hi All, I posted some code for calling IronRuby code I found in the  
mailing  
list onto the wiki.

[http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp](http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp)

RubyEngine re = RubyEngine.CurrentEngine;

string script = “s = ‘let us get started’” + Environment.NewLine

```
+ "i = s.length");

```

re.ExecuteCommand(script);

The only thing is, I don’t know how to pass a .NET object to the context  
of  
the RubyEngine so it can be called from within the script. I’d like to  
update the example with that.

Any help?

Phil

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [October 1, 2007, 4:23am UTC](https://www.ruby-forum.com/t/passing-a-net-object-to-the-rubyengine/115760/15 "2007-10-01T04:23:55Z")

</div>

Josh Nursing wrote:

> BTW, Charles,
> 
> congrats on the release of JRuby.

Well, it isn’t a new release yet; the current release is still the 1.0.1  
maintenance release we cut this summer. I just blogged about the  
compiler being complete, and it will be shipped with the 1.1 release  
(candidate) in November.

Thanks for the congrats though 🙂

- Charlie
