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

**URL:** <https://www.ruby-forum.com/t/could-you-please-teach-a-canonical-style-for-writing-the-ironruby-hosting-code/179583>\
**Category:** IronRuby\
**Created:** [November 29, 2009, 3:11pm UTC](https://www.ruby-forum.com/t/could-you-please-teach-a-canonical-style-for-writing-the-ironruby-hosting-code/179583 "2009-11-29T15:11:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSBSOS7SbSHS1SsSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@SSSBSOS7SbSHS1SsSSS](https://www.ruby-forum.com/u/SSSBSOS7SbSHS1SsSSS)\
**Post date:** [November 29, 2009, 3:11pm UTC](https://www.ruby-forum.com/t/could-you-please-teach-a-canonical-style-for-writing-the-ironruby-hosting-code/179583/1 "2009-11-29T15:11:43Z")

</div>

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.

[BACKGROUND]  
Now, I’m tring to host the IronRuby 1.0RC on C#.NET (Visual Studio  
2008).  
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  
yet.

[QUESTION]  
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#
    IronRuby.Ruby.GetExecutionContext(engine).DefineGlobalVariable("xxx",

```

789);

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

```

#{x}. $xxx is #{$xxx}."" ");  
source.Execute(scope);

```
    Console.ReadLine();
}

```

Is it a canonical style?

Thank you.  
B.R. Kenichi HASHIMOTO

---

<div class="post-metadata">

**Author:** ![SSSBSOS7SbSHS1SsSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@SSSBSOS7SbSHS1SsSSS](https://www.ruby-forum.com/u/SSSBSOS7SbSHS1SsSSS)\
**Post date:** [November 29, 2009, 4:27pm UTC](https://www.ruby-forum.com/t/could-you-please-teach-a-canonical-style-for-writing-the-ironruby-hosting-code/179583/2 "2009-11-29T16:27:47Z")

</div>

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 [dlr.codeplex.com](http://dlr.codeplex.com) 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).

~Jimmy  
Sent from my phone

On Nov 29, 2009, at 9:11 AM, “ã¯ã—ã‚‚ã¨ã‘ã‚“ã„ã¡”

---

<div class="post-metadata">

**Author:** ![SSSBSOS7SbSHS1SsSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@SSSBSOS7SbSHS1SsSSS](https://www.ruby-forum.com/u/SSSBSOS7SbSHS1SsSSS)\
**Post date:** [December 1, 2009, 6:26pm UTC](https://www.ruby-forum.com/t/could-you-please-teach-a-canonical-style-for-writing-the-ironruby-hosting-code/179583/3 "2009-12-01T18:26:50Z")

</div>

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

Tomas

---

<div class="post-metadata">

**Author:** ![SSSBSOS7SbSHS1SsSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@SSSBSOS7SbSHS1SsSSS](https://www.ruby-forum.com/u/SSSBSOS7SbSHS1SsSSS)\
**Post date:** [December 1, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/could-you-please-teach-a-canonical-style-for-writing-the-ironruby-hosting-code/179583/4 "2009-12-01T15:44:42Z")

</div>

Hello Jimmy-sann

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

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  
ScriptSource.  
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  
complie,execution.

* * *

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.  
B.R. Kenichi HASHIMOTO

2009/11/30 Jimmy S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
