# How to transfer scriptscrope to a RubyCommandLine

**URL:** <https://www.ruby-forum.com/t/how-to-transfer-scriptscrope-to-a-rubycommandline/188534>\
**Category:** IronRuby\
**Created:** [May 13, 2010, 3:22pm UTC](https://www.ruby-forum.com/t/how-to-transfer-scriptscrope-to-a-rubycommandline/188534 "2010-05-13T15:22:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ray\_linn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@ray\_linn](https://www.ruby-forum.com/u/ray_linn)\
**Post date:** [May 13, 2010, 3:22pm UTC](https://www.ruby-forum.com/t/how-to-transfer-scriptscrope-to-a-rubycommandline/188534/1 "2010-05-13T15:22:35Z")

</div>

I found Ironruby 1.0 change a lot for some APIs, at previous version

I can create a rubycommandline via C# like this

IronRuby.Hosting.RubyCommandLine rubycommandline = new  
RubyCommandLine(new  
IronRuby.Runtime.RubyContext(ScriptDomainManager.CurrentManager));

but now the construction accept zero parameters, so How transfer a  
scriptscope to RubyCommandLine…?

I used debug to verbose the instance of RubyCommandLine, its scriptscope  
is always null!

---

<div class="post-metadata">

**Author:** ![ray\_linn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@ray\_linn](https://www.ruby-forum.com/u/ray_linn)\
**Post date:** [May 13, 2010, 4:15pm UTC](https://www.ruby-forum.com/t/how-to-transfer-scriptscrope-to-a-rubycommandline/188534/2 "2010-05-13T16:15:06Z")

</div>

Ray L. wrote:

> I found Ironruby 1.0 change a lot for some APIs, at previous version
> 
> I can create a rubycommandline via C# like this
> 
> IronRuby.Hosting.RubyCommandLine rubycommandline = new  
> RubyCommandLine(new  
> IronRuby.Runtime.RubyContext(ScriptDomainManager.CurrentManager));
> 
> but now the construction accept zero parameters, so How transfer a  
> scriptscope to RubyCommandLine..?
> 
> I used debug to verbose the instance of RubyCommandLine, its scriptscope  
> is always null!

2nd question why global variable does not work?

## Form frm = new Form(); ScriptEngine ruby = IronRuby.Ruby.GetEngine(IronRuby.Ruby.CreateRuntime()); ruby.Runtime.Globals.SetVariable(“frm”, frm); ScriptScope scriptscope = ruby.Runtime.CreateScope(); ScriptSource script = this.scriptengine.CreateScriptSourceFromFile(“test.rb”); scriptscope.SetVariable(“frm”,frm); ruby.Execute(script.GetCode(),scriptscope);

test.rb

defined? $frm  
---------but it is nil…  
defied? frm  
it is ok…

---

<div class="post-metadata">

**Author:** ![ray\_linn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@ray\_linn](https://www.ruby-forum.com/u/ray_linn)\
**Post date:** [May 13, 2010, 6:31pm UTC](https://www.ruby-forum.com/t/how-to-transfer-scriptscrope-to-a-rubycommandline/188534/3 "2010-05-13T18:31:16Z")

</div>

Ruby global variables are not accessible via hosting API.  
ScriptRuntime.Globals refers to global constants (constants defined on  
Object).

ScriptEngine ruby = IronRuby.Ruby.CreateEngine();  
ruby.Runtime.Globals.SetVariable(“Bar”, 2);  
ScritpScope scope = ruby.CreateScope();  
scope.SetVariable(“frm”, 123);  
ruby.ExecuteFile(“test.rb”, file);

where test.rb is  
p frm, defined?(frm), p Bar

prints  
123  
Nil  
2

frm is looked up via method\_missing on the main object.

Tomas

-----Original Message-----  
From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Ray L.  
Sent: Thursday, May 13, 2010 7:15 AM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Ironruby-core] How to transfer scriptscrope to a  
RubyCommandLine

Ray L. wrote:

> I used debug to verbose the instance of RubyCommandLine, its  
> scriptscope is always null!

2nd question why global variable does not work?

## Form frm = new Form(); ScriptEngine ruby = IronRuby.Ruby.GetEngine(IronRuby.Ruby.CreateRuntime()); ruby.Runtime.Globals.SetVariable(“frm”, frm); ScriptScope scriptscope = ruby.Runtime.CreateScope(); ScriptSource script = this.scriptengine.CreateScriptSourceFromFile(“test.rb”); scriptscope.SetVariable(“frm”,frm); ruby.Execute(script.GetCode(),scriptscope);

test.rb

## defined? $frm ---------but it is nil… defied? frm it is ok…

Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

* * *

Ironruby-core mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

---

<div class="post-metadata">

**Author:** ![ray\_linn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@ray\_linn](https://www.ruby-forum.com/u/ray_linn)\
**Post date:** [May 14, 2010, 7:50am UTC](https://www.ruby-forum.com/t/how-to-transfer-scriptscrope-to-a-rubycommandline/188534/4 "2010-05-14T07:50:35Z")

</div>

Tomas M. wrote:

> Can you describe your scenario where you use the command line? What  
> features you need? It’s relatively easy to implement a command line  
> yourself (see e.g.  
> [http://blog.tomasm.net/2009/04/15/python-says-hello-to-ruby/](http://blog.tomasm.net/2009/04/15/python-says-hello-to-ruby/)). The APIs  
> in Microsoft.Hosting.Shell are mostly legacy and I wouldn’t recommend  
> using them.
> 
> Tomas

I tried to store a WinForm in my script scope, and if i could transfer  
the script scope to the command line, that I have a chance to operate  
this WinForm in ruby console. As an example,I could change the title of  
the WinForm in command line like this:

$frm.text=“hello,world”

seems in ironpython (refer to  
[Exposing global variables in IronPython – The Stochastic Game](http://ludovic.chabant.com/devblog/2009/12/24/exposing-global-variables-in-ironpython/))

I can get the global variables via import something, but to ironruby  
whatever require or include, I can not import a global variables.

---

<div class="post-metadata">

**Author:** ![ray\_linn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@ray\_linn](https://www.ruby-forum.com/u/ray_linn)\
**Post date:** [May 14, 2010, 7:55am UTC](https://www.ruby-forum.com/t/how-to-transfer-scriptscrope-to-a-rubycommandline/188534/5 "2010-05-14T07:55:34Z")

</div>

Tomas M. wrote:

> Can you describe your scenario where you use the command line? What  
> features you need? It’s relatively easy to implement a command line  
> yourself (see e.g.  
> [http://blog.tomasm.net/2009/04/15/python-says-hello-to-ruby/](http://blog.tomasm.net/2009/04/15/python-says-hello-to-ruby/)). The APIs  
> in Microsoft.Hosting.Shell are mostly legacy and I wouldn’t recommend  
> using them.
> 
> Tomas

Hi, Tomas

Thanks a lot for your response.

Here is an interesting example I found, but after upgrade to Ir 1.0  
(.net 4) , it does not work now

> **[Michael.NET: (Semi) Practical IronRuby](http://michaeldotnet.blogspot.com/2008/01/semi-practical-ironruby.html)**

Thanks  
Ray L.

---

<div class="post-metadata">

**Author:** ![ray\_linn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@ray\_linn](https://www.ruby-forum.com/u/ray_linn)\
**Post date:** [May 14, 2010, 8:24am UTC](https://www.ruby-forum.com/t/how-to-transfer-scriptscrope-to-a-rubycommandline/188534/6 "2010-05-14T08:24:45Z")

</div>

Tomas M. wrote:

> Can you describe your scenario where you use the command line? What  
> features you need? It’s relatively easy to implement a command line  
> yourself (see e.g.  
> [http://blog.tomasm.net/2009/04/15/python-says-hello-to-ruby/](http://blog.tomasm.net/2009/04/15/python-says-hello-to-ruby/)). The APIs  
> in Microsoft.Hosting.Shell are mostly legacy and I wouldn’t recommend  
> using them.
> 
> Tomas

Thanks Tomas

At last, I found a thread you replied previously. if I used

```
        RubyContext context = GetExecutionContext(ruby);
        context.DefineGlobalVariable("logowin",logowin);

```

it works as expected.. this made me a little confused, is the

context.DefineGlobalVariable

the same as

ruby.Runtime.Globals.SetVariable

the naming is just similiar and drive me crazy..

Regards  
Ray

---

<div class="post-metadata">

**Author:** ![ray\_linn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@ray\_linn](https://www.ruby-forum.com/u/ray_linn)\
**Post date:** [May 13, 2010, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-transfer-scriptscrope-to-a-rubycommandline/188534/7 "2010-05-13T18:45:19Z")

</div>

Can you describe your scenario where you use the command line? What  
features you need? It’s relatively easy to implement a command line  
yourself (see e.g.  
[tomasm.net](http://blog.tomasm.net/2009/04/15/python-says-hello-to-ruby/)). The APIs  
in Microsoft.Hosting.Shell are mostly legacy and I wouldn’t recommend  
using them.

Tomas
