This actually should not work. The InvokeMember call below invokes "add" on nil 
object. It should be able to invoke any public method on NilClass, Object, or 
Kernel. Although "add" is defined on Object it is private as you can see by 
executing script:

Engine.Execute(@"
def add(a,b); a + b; end
p Object.private_instance_methods(false).sort
");

We publish add method to the local DLR scope (if given) where you can read it 
from like so:

ScriptEngine engine = IronRuby.Ruby.CreateEngine();
var scope = engine.CreateScope();
engine.Execute("def add(a,b); a + b; end", scope);
var add = scope.GetVariable<Func<int, int, int>>("add");
Console.WriteLine(add(1,2)); // Print 3

or, since scope is  a dynamic object, you can also invoke the method on it:

ScriptEngine engine = IronRuby.Ruby.CreateEngine();
var scope = engine.CreateScope();
engine.Execute("def add(a,b); a + b; end", scope);
object o = engine.Operations.InvokeMember(scope, "add", 1, 2);
Console.WriteLine(o); // Print 3

Note that, whenever you execute against a DLR scope you are essentially 
instance-eval'ing the code against the scope object.
Hence the top-level methods are defined on scope, not on Object.

This way executions against different scopes don't step on each other:
    var scope1 = Engine.CreateScope();
    var scope2 = Engine.CreateScope();
    Engine.Execute("def foo(a,b); a + b; end", scope1);
    Engine.Execute("def foo(a,b); a - b; end", scope2);

    Assert(Engine.Execute<int>("foo(1,2)", scope1) == 3);
    Assert(Engine.Execute<int>("foo(1,2)", scope2) == -1);

I hope this is not too confusing.

Tomas

From: ironruby-core-boun...@rubyforge.org 
[mailto:ironruby-core-boun...@rubyforge.org] On Behalf Of Shay Friedman
Sent: Thursday, October 01, 2009 6:31 AM
To: ironruby-core@rubyforge.org
Subject: Re: [Ironruby-core] calling non class methods from C#

They are located on the global scope, so just pass null as the object. For 
example, the next code executes the add method that is defined on the global 
scope:

ScriptEngine engine = IronRuby.Ruby.CreateEngine();
engine.Execute("def add(a,b); a + b; end");
object o = engine.Operations.InvokeMember(null, "add", 1, 2);
Console.WriteLine(o); // Print 3

Shay.
----------------------------
Shay Friedman
Author of IronRuby Unleashed
http://www.IronShay.com
Follow me: http://twitter.com/ironshay
On Thu, Oct 1, 2009 at 2:58 PM, Eelco Henderichs 
<li...@ruby-forum.com<mailto:li...@ruby-forum.com>> wrote:
Dear users,

I have found how to invoke class methods and use its result in the C#
application.

However I can't seem to find how to invoke methodes located outside of a
class. So I started wondering if this is at all possible. If it is, I
would like to know how.

Thanks in advance
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Ironruby-core mailing list
Ironruby-core@rubyforge.org<mailto:Ironruby-core@rubyforge.org>
http://rubyforge.org/mailman/listinfo/ironruby-core

_______________________________________________
Ironruby-core mailing list
Ironruby-core@rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core

Reply via email to