Could you please teach a canonical style for writing the IronRuby hosting code

Hello, I’m kenichi hashimoto.
I’m a Japanese. I don’t write english well.

Now I have a question about a canonical style for writing the IronRuby
hosting code.

Now, I’m tring to host the IronRuby 1.0RC on C#.NET (Visual Studio
And, I have been trying to host it since IronRuby 0.5.
However, I haven’t understood about how to write code in right style

Could you please teach a canonical style for writing the IronRuby
hosting code.

Now, I wrote below:

static void Main(string[] args)
    // create the IronRuby engine.
    ScriptRuntime runtime = IronRuby.Ruby.CreateRuntime();
    var engine = runtime.GetEngine("rb");

    // create the scope and set the local variable from C#
    var scope = engine.CreateScope();
    scope.SetVariable("x", 123);

    // set the global variable from C#


    // execute the script with the scope
    var source = engine.CreateScriptSourceFromString(@"puts ""x is

#{x}. $xxx is #{$xxx}."" ");


Is it a canonical style?

Thank you.

That looks good for hosting code that can switch between languages
easily. If you’re just using Ruby, you can use
IronRuby.Ruby.CreateEngine() instead of getting the engine by string,
but I’d suggest staying with what you have; you can change to other
languages easily.

For running code from strings, you don’t necessarily need to create a
ScriptSource; you can use engine.Execute(string, ScriptScope) instead.
Creating a script source are for the cases where you want to compile
the code and store it before executing, or you want to change the mode
of source code execution (file, interactive, expressions, etc).

There is a dlr hosting spec on that shows various
hosting scenarios and how the code should look.

Let us know if you have any other questions about hosting. I showed
hosting at RubyConf this year, so keep an eye out for that blog post
(I’ll send mail to this list when it’s done).

Sent from my phone

On Nov 29, 2009, at 9:11 AM, “はしもとけんいち”

Runtime.Globals.SetVariable(‘C’, …) sets a global constant C, not a
Ruby global variable $C.


Hello Jimmy-sann

Thank you for your some advice about my code,
and for giving me an information about the link to documentation of

I understood your advices as below:

  1. for creating the engine.
    If the program hosts the ONLY IronRuby, ‘IronRuby.Ruby.CreateEngine()’
    is better than ‘runtime.GetEngine(“rb”);’.
    If the program needs to host many dynamic languages, ‘var engine =
    ScriptRuntime.CreateFromConfiguration().GetEngine(“rb”);’ is better
    than ‘IronRuby.Ruby.CreateEngine()’.

  2. ScriptSource
    If the program evaluates same script and never execute same script,
    ‘engine.Execute(string, ScriptScope)’ is better than for creating the
    Because, engine.Execute(string, ScriptScope) is simple code.

If the program evaluates one script many times, for creating the
ScriptSource is better than ‘engine.Execute(string, ScriptScope)’.
Because creating a ScriptSource compiles a ruby code ONLY once time
and reduces .
Of course, when creating a ScriptSource, I can change the mode for

Now, I have one more question about global variables.
First, in my last mail, I used
‘IronRuby.Ruby.GetExecutionContext(engine).DefineGlobalVariable’ for
defining global variable.
Now I found the way to write ‘runtime.Globals.SetVariable(“xxx”,
567);’ to define global variable in the documentaion , but it seems
NOT work.
Is this a bug?

Thank you.

2009/11/30 Jimmy S. [email protected]: