Calling non class methods from C#

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

class TheExample
def output
“Hello World”
end

end

var operations = Engine.CreateOperations();

Engine.ExecuteFile(/path/to/example.rb);

var instance = operations.CreateInstance();
var result = operations.InvokeMember(instance, “output”);


Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

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 F.
Author of IronRuby Unleashed
http://www.IronShay.com
Follow me: http://twitter.com/ironshay

Oke thanks for the information. Not sure if I fully understand how to do
this. But it surely gave me someting to work with and I can start
experimenting with it.

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: [email protected]
[mailto:[email protected]] On Behalf Of Shay F.
Sent: Thursday, October 01, 2009 6:31 AM
To: [email protected]
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 F.
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
<[email protected]mailto:[email protected]> 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/.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs