I updated the demo code in the wiki based on the code snippet here.
http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp
IÂ’ll try and make sure it stays up to date. J
Phil
From: [email protected]
[mailto:[email protected]] On Behalf Of Tomas M.
Sent: Friday, September 28, 2007 12:46 PM
To: [email protected]
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: [email protected]
[mailto:[email protected]] On Behalf Of Cory F.
Sent: Friday, September 28, 2007 8:02 AM
To: [email protected]
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: [email protected]
[mailto:[email protected]] On Behalf Of Stefan
Hüttenrauch
Sent: Friday, September 28, 2007 10:49 AM
To: [email protected]
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: [email protected]
[mailto:[email protected]] On Behalf Of Eric N.
Sent: Freitag, 28. September 2007 15:40
To: [email protected]
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?
On 9/28/07, Eric N. < [email protected]
mailto:[email protected] > 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:
- var.to_s is necessary because I don’t think IR is coercing CLR
Strings to
Ruby Strings yet.
- 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. [email protected] 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
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