Jim's right. Each time you execute a top-level code via hosting API a new 
"main" singleton object is created and the code is executed against that 
object. The object is associated with the DLR scope. Unless you provide one the 
scope is also created each time you call Execute method. ExecuteFile returns 
the scope the file was executed against (unless you specify one as a 
parameter). This should work:

ScriptEngine engine = Ruby.CreateEngine();
ScriptScope scope = engine.ExecuteFile(file);

var code = "private_methods(false)";
var privates = engine.Execute(code, scope);

var code2 = "method :instance_variable_get";
var action = engine.Execute(code2, scope);
var result = engine.Operations.Call(action, "@expose");

engine.Execute(code, scope) is a shortcut for 
engine.CreateScriptFromString(code).Execute(scope)

If you just want to get delegates to top-level methods, you can do it like so:

ScriptEngine engine = Ruby.CreateEngine();
ScriptScope scope = engine.ExecuteFile(file);
var testFoo = scope.GetVariable<Func<string>>("testFoo")
var testBar = scope.GetVariable<Func<string>>("testBar")

Console.WriteLine(testFoo());   // => foo
Console.WriteLine(testBar());   // => bar

Tomas


From: ironruby-core-boun...@rubyforge.org 
[mailto:ironruby-core-boun...@rubyforge.org] On Behalf Of Jim Deville
Sent: Saturday, August 15, 2009 8:14 AM
To: ironruby-core@rubyforge.org
Subject: Re: [Ironruby-core] Top Level Variables and Methods

You'll need a scope. To get the scope object, it's engine.CreateScope(). Then 
you need to pass that scope object to the engine.Execute method calls as a 
second parameter.

You may need to initialize the top level binding as well. Tomas can tell you 
for sure.

JD


________________________________
From: David Blackmon <davidkblack...@gmail.com>
Sent: August 15, 2009 7:37 AM
To: ironruby-core@rubyforge.org <ironruby-core@rubyforge.org>
Subject: [Ironruby-core] Top Level Variables and Methods
_______________________________________________
Ironruby-core mailing list
Ironruby-core@rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core

Reply via email to