# Calling non class methods from C#

**URL:** <https://www.ruby-forum.com/t/calling-non-class-methods-from-c/176517>\
**Category:** IronRuby\
**Created:** [October 1, 2009, 2:58pm UTC](https://www.ruby-forum.com/t/calling-non-class-methods-from-c/176517 "2009-10-01T14:58:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skyrebel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@skyrebel](https://www.ruby-forum.com/u/skyrebel)\
**Post date:** [October 1, 2009, 2:58pm UTC](https://www.ruby-forum.com/t/calling-non-class-methods-from-c/176517/1 "2009-10-01T14:58:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![skyrebel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@skyrebel](https://www.ruby-forum.com/u/skyrebel)\
**Post date:** [October 1, 2009, 3:32pm UTC](https://www.ruby-forum.com/t/calling-non-class-methods-from-c/176517/2 "2009-10-01T15:32:01Z")

</div>

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](http://flanders.co.nz)  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

---

<div class="post-metadata">

**Author:** ![skyrebel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@skyrebel](https://www.ruby-forum.com/u/skyrebel)\
**Post date:** [October 1, 2009, 3:32pm UTC](https://www.ruby-forum.com/t/calling-non-class-methods-from-c/176517/3 "2009-10-01T15:32:02Z")

</div>

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](http://www.IronShay.com)  
Follow me: [http://twitter.com/ironshay](http://twitter.com/ironshay)

---

<div class="post-metadata">

**Author:** ![skyrebel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@skyrebel](https://www.ruby-forum.com/u/skyrebel)\
**Post date:** [October 2, 2009, 9:23am UTC](https://www.ruby-forum.com/t/calling-non-class-methods-from-c/176517/4 "2009-10-02T09:23:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![skyrebel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@skyrebel](https://www.ruby-forum.com/u/skyrebel)\
**Post date:** [October 2, 2009, 12:00am UTC](https://www.ruby-forum.com/t/calling-non-class-methods-from-c/176517/5 "2009-10-02T00:00:11Z")

</div>

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: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Shay F.  
Sent: Thursday, October 01, 2009 6:31 AM  
To: removed\_email\_address@domain.invalid  
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](http://www.IronShay.com)  
Follow me: [http://twitter.com/ironshay](http://twitter.com/ironshay)  
On Thu, Oct 1, 2009 at 2:58 PM, Eelco Henderichs  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> 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/](http://www.ruby-forum.com/).
