# Accessing IronRuby method from C#

**URL:** <https://www.ruby-forum.com/t/accessing-ironruby-method-from-c/126894>\
**Category:** IronRuby\
**Created:** [January 23, 2008, 6:17am UTC](https://www.ruby-forum.com/t/accessing-ironruby-method-from-c/126894 "2008-01-23T06:17:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [January 23, 2008, 6:17am UTC](https://www.ruby-forum.com/t/accessing-ironruby-method-from-c/126894/1 "2008-01-23T06:17:36Z")

</div>

Greetings to the mighty IronRuby experts from a humble developer! 🙂

I am testing the latest CVS snapshot (v. 73, downloaded an hour ago) of  
IronRuby.

We are evaluating IronRuby for possible usage in WPF project.

First, I want to test a simple case of defining a method in Ruby and  
calling the method later from C#.

Below I copy a test program that tries this in two ways (methods  
AccessMethod1 and AccessMethod2).  
Both attempts fail with exceptions that are written as comments in the  
code.

I understand that the DLR and IronRuby are in a flux at the moment but I  
still suppose there must be some working way to perform such trivial  
interop task…?

Thanks in advance!

Robert B.  
Software architect  
Napa Ltd

* * *

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using Microsoft.Scripting;  
using Microsoft.Scripting.Hosting;  
using Ruby.Runtime;  
using Ruby.Builtins;

namespace IronRubyTests  
{  
class Program  
{  
private IScriptEnvironment \_scriptRuntime;  
public IScriptEnvironment ScriptRuntime { get { return  
this.\_scriptRuntime; } }  
public IScriptEngine RubyEngine { get { return  
ScriptRuntime.GetEngine(typeof(RubyContext)); } }  
public ObjectOperations Operations { get { return  
RubyEngine.Operations; } }  
public RubyExecutionContext ExecutionContext { get { return  
Ruby.IronRuby.GetExecutionContext(RubyEngine); } }  
public IAttributesCollection GlobalItems { get { return  
ExecutionContext.GlobalScope.Dict; } }

```
    delegate void TestDelegate();

    static void Main(string[] args)
    {
        new Program().RunAllTests();
    }

    public Program()
    {
        this._scriptRuntime = Ruby.IronRuby.CreateRuntime();
    }

    public void RunAllTests()
    {
        List<TestDelegate> tests = new List<TestDelegate> {
            new TestDelegate(SimpleTests), new

```

TestDelegate(AccessMethod1), new TestDelegate(AccessMethod2)  
};  
foreach (TestDelegate test in tests)  
{  
Console.WriteLine("================== EXECUTING: {0}  
====================", test.Method.Name);  
try { test(); }  
catch (Exception ex) { Console.WriteLine(ex); }  
}  
}

```
    public IScriptScope ExecuteString(string code)
    {
        return ScriptRuntime.ExecuteSourceUnit(
            RubyEngine.CreateScriptSourceFromString(code));
    }

    public void SetGlobalVar(string varName, object value)
    {

```

ExecutionContext.GlobalVariables[SymbolTable.StringToId(varName)] =  
value;  
}

```
    public object GetGlobalVar(string varName)
    {
        return

```

ExecutionContext.GlobalVariables[SymbolTable.StringToId(varName)];  
}

```
    public object GetGlobalConst(string name)
    {
        return

```

GlobalItems.SymbolAttributes[SymbolTable.StringToId(name)];  
}

```
    public void SimpleTests()
    {
        ExecuteString("puts 'RUBY SAYS HELLO'"); // OK
        SetGlobalVar("x", 666); // OK
        ExecuteString("$y=555"); // OK
        ExecuteString("puts \"ruby: $x = #{$x}\""); // OK
    }

    public void AccessMethod1()
    {
        // Following fails with System.MissingMethodException:

```

undefined local variable or method ‘method’ for main:Object  
ExecuteString(@"  
def sayHello  
puts ‘Hello from method!’  
end

```
            $m = method(:sayHello)
        ");

        // Get the method object:
        object m = GetGlobalVar("m");

        // Here call the method, etc.
    }

    public void AccessMethod2()
    {
        ExecuteString(@"
          module MyMethods
            def sayHello
               puts 'Hello from method!'
            end
          end
        ");

        RubyModule myModule = (RubyModule)

```

GetGlobalConst(“MyMethods”); // OK  
object sayHello = Operations.GetMember(myModule,  
“sayHello”); // Fails at Assertion at  
ActionBinber.UpdateSiteAndExecute()

```
        // Here call the method, etc.
    }

} // class

```

} // namespace

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [January 23, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/accessing-ironruby-method-from-c/126894/2 "2008-01-23T15:59:45Z")

</div>

Today we don’t support unbound methods yet (what you are trying to do  
via the call to the method function). If you want first class functions,  
Proc.new and lambda work just fine today.

I’ll let Tomas chime in on AccessMethod2().

Thanks,  
-John

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Robert  
Brotherus  
Sent: Tuesday, January 22, 2008 8:20 AM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] Accessing IronRuby method from C#

Greetings to the mighty IronRuby experts from a humble developer! 🙂

I am testing the latest CVS snapshot (v. 73, downloaded an hour ago) of  
IronRuby.

We are evaluating IronRuby for possible usage in WPF project.

First, I want to test a simple case of defining a method in Ruby and  
calling the method later from C#.

Below I copy a test program that tries this in two ways (methods  
AccessMethod1 and AccessMethod2).  
Both attempts fail with exceptions that are written as comments in the  
code.

I understand that the DLR and IronRuby are in a flux at the moment but I  
still suppose there must be some working way to perform such trivial  
interop task…?

Thanks in advance!

Robert B.  
Software architect  
Napa Ltd

* * *

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using Microsoft.Scripting;  
using Microsoft.Scripting.Hosting;  
using Ruby.Runtime;  
using Ruby.Builtins;

namespace IronRubyTests  
{  
class Program  
{  
private IScriptEnvironment \_scriptRuntime;  
public IScriptEnvironment ScriptRuntime { get { return  
this.\_scriptRuntime; } }  
public IScriptEngine RubyEngine { get { return  
ScriptRuntime.GetEngine(typeof(RubyContext)); } }

```
    public ObjectOperations Operations { get { return 

```

RubyEngine.Operations; } }  
public RubyExecutionContext ExecutionContext { get { return  
Ruby.IronRuby.GetExecutionContext(RubyEngine); } }

```
    public IAttributesCollection GlobalItems { get { return 

```

ExecutionContext.GlobalScope.Dict; } }

```
    delegate void TestDelegate();

    static void Main(string[] args)
    {
        new Program().RunAllTests();
    }

    public Program()
    {
        this._scriptRuntime = Ruby.IronRuby.CreateRuntime();
    }

    public void RunAllTests()
    {
        List<TestDelegate> tests = new List<TestDelegate> {
            new TestDelegate(SimpleTests), new 

```

TestDelegate(AccessMethod1), new TestDelegate(AccessMethod2)  
};  
foreach (TestDelegate test in tests)  
{  
Console.WriteLine(“================== EXECUTING: {0}  
====================”, test.Method.Name);  
try { test(); }  
catch (Exception ex) { Console.WriteLine(ex); }  
}  
}

```
    public IScriptScope ExecuteString(string code)
    {
        return ScriptRuntime.ExecuteSourceUnit(
            RubyEngine.CreateScriptSourceFromString(code));
    }

    public void SetGlobalVar(string varName, object value)
    {
        ExecutionContext.GlobalVariables[SymbolTable.StringToId(varName)] 

```

= value;  
}

```
    public object GetGlobalVar(string varName)
    {
        return 

```

ExecutionContext.GlobalVariables[SymbolTable.StringToId(varName)];  
}

```
    public object GetGlobalConst(string name)
    {
        return 

```

GlobalItems.SymbolAttributes[SymbolTable.StringToId(name)];  
}

```
    public void SimpleTests()
    {
        ExecuteString("puts 'RUBY SAYS HELLO'"); // OK
        SetGlobalVar("x", 666); // OK
        ExecuteString("$y=555"); // OK
        ExecuteString("puts \"ruby: $x = #{$x}\""); // OK
    }

    public void AccessMethod1()
    {
        // Following fails with System.MissingMethodException: 

```

undefined local variable or method ‘method’ for main:Object

```
        ExecuteString(@"
            def sayHello
               puts 'Hello from method!'
            end

            $m = method(:sayHello)
        ");

        // Get the method object:
        object m = GetGlobalVar("m");

        // Here call the method, etc.
    }

    public void AccessMethod2()
    {
        ExecuteString(@"
          module MyMethods
            def sayHello
               puts 'Hello from method!'
            end
          end
        ");

        RubyModule myModule = (RubyModule) 

```

GetGlobalConst(“MyMethods”); // OK  
object sayHello = Operations.GetMember(myModule,  
“sayHello”); // Fails at Assertion at  
ActionBinber.UpdateSiteAndExecute()

```
        // Here call the method, etc.
    }

} // class

```

} // namespace

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [January 24, 2008, 9:54am UTC](https://www.ruby-forum.com/t/accessing-ironruby-method-from-c/126894/3 "2008-01-24T09:54:22Z")

</div>

Thanks John for your reply!

Still not getting my method calls to work from C# with lambda either:

[TestMethod]  
public void AccessMethod3()  
{  
ExecuteString(@"  
$m = lambda do |par|  
puts 'Hello from lambda: ’ + par  
end  
");  
Proc m = (Proc) GetGlobalVar(“m”); // OK  
Operations.Call(m, “abc”); // InvalidCast: Cannot cast Proc to  
BlockParam  
Delegate1 d = Operations.ConvertTo(m); // OK  
d(“abc”); // InvalidCast: Cannot cast Proc to BlockParam  
}

Robert B.  
Software architect  
Napa Ltd  
Tammasaarenkatu 3, Helsinki FI-00180  
P.O.Box 470, Helsinki FI-00181

Tel. +358 9 22 813 1  
Direct. +358 9 22 813 611  
GSM +358 45 11 456 02  
Fax. +358 9 22 813 800

Email: removed\_email\_address@domain.invalid [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[www.napa.fi](http://www.napa.fi) [http://www.napa.fi/](http://www.napa.fi/)

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of John L. (DLR)  
Sent: 23. tammikuuta 2008 16:59  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Accessing IronRuby method from C#

Today we don’t support unbound methods yet (what you are trying to do  
via the call to the method function). If you want first class functions,  
Proc.new and lambda work just fine today.

I’ll let Tomas chime in on AccessMethod2().

Thanks,

-John

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Robert  
Brotherus  
Sent: Tuesday, January 22, 2008 8:20 AM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] Accessing IronRuby method from C#

Greetings to the mighty IronRuby experts from a humble developer! 🙂

I am testing the latest CVS snapshot (v. 73, downloaded an hour ago) of  
IronRuby.

We are evaluating IronRuby for possible usage in WPF project.

First, I want to test a simple case of defining a method in Ruby and  
calling the method later from C#.

Below I copy a test program that tries this in two ways (methods  
AccessMethod1 and AccessMethod2).  
Both attempts fail with exceptions that are written as comments in the  
code.

I understand that the DLR and IronRuby are in a flux at the moment but I  
still suppose there must be some working way to perform such trivial  
interop task…?

Thanks in advance!

Robert B.  
Software architect  
Napa Ltd

* * *

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using Microsoft.Scripting;  
using Microsoft.Scripting.Hosting;  
using Ruby.Runtime;  
using Ruby.Builtins;

namespace IronRubyTests  
{  
class Program  
{  
private IScriptEnvironment \_scriptRuntime;  
public IScriptEnvironment ScriptRuntime { get { return  
this.\_scriptRuntime; } }  
public IScriptEngine RubyEngine { get { return  
ScriptRuntime.GetEngine(typeof(RubyContext)); } }

```
    public ObjectOperations Operations { get { return

```

RubyEngine.Operations; } }  
public RubyExecutionContext ExecutionContext { get { return  
Ruby.IronRuby.GetExecutionContext(RubyEngine); } }

```
    public IAttributesCollection GlobalItems { get { return

```

ExecutionContext.GlobalScope.Dict; } }

```
    delegate void TestDelegate();

    static void Main(string[] args)
    {
        new Program().RunAllTests();
    }

    public Program()
    {
        this._scriptRuntime = Ruby.IronRuby.CreateRuntime();
    }

    public void RunAllTests()
    {
        List<TestDelegate> tests = new List<TestDelegate> {
            new TestDelegate(SimpleTests), new

```

TestDelegate(AccessMethod1), new TestDelegate(AccessMethod2)  
};  
foreach (TestDelegate test in tests)  
{  
Console.WriteLine(“================== EXECUTING: {0}  
====================”, test.Method.Name);  
try { test(); }  
catch (Exception ex) { Console.WriteLine(ex); }  
}  
}

```
    public IScriptScope ExecuteString(string code)
    {
        return ScriptRuntime.ExecuteSourceUnit(
            RubyEngine.CreateScriptSourceFromString(code));
    }

    public void SetGlobalVar(string varName, object value)
    {

```

ExecutionContext.GlobalVariables[SymbolTable.StringToId(varName)] =  
value;  
}

```
    public object GetGlobalVar(string varName)
    {
        return

```

ExecutionContext.GlobalVariables[SymbolTable.StringToId(varName)];  
}

```
    public object GetGlobalConst(string name)
    {
        return

```

GlobalItems.SymbolAttributes[SymbolTable.StringToId(name)];  
}

```
    public void SimpleTests()
    {
        ExecuteString("puts 'RUBY SAYS HELLO'"); // OK
        SetGlobalVar("x", 666); // OK
        ExecuteString("$y=555"); // OK
        ExecuteString("puts \"ruby: $x = #{$x}\""); // OK
    }

    public void AccessMethod1()
    {
        // Following fails with System.MissingMethodException:

```

undefined local variable or method ‘method’ for main:Object

```
        ExecuteString(@"
            def sayHello
               puts 'Hello from method!'
            end

            $m = method(:sayHello)
        ");

        // Get the method object:
        object m = GetGlobalVar("m");

        // Here call the method, etc.
    }

    public void AccessMethod2()
    {
        ExecuteString(@"
          module MyMethods
            def sayHello
               puts 'Hello from method!'
            end
          end
        ");

        RubyModule myModule = (RubyModule)

```

GetGlobalConst(“MyMethods”); // OK  
object sayHello = Operations.GetMember(myModule,  
“sayHello”); // Fails at Assertion at  
ActionBinber.UpdateSiteAndExecute()

```
        // Here call the method, etc.
    }

} // class

```

} // namespace

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [January 24, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/accessing-ironruby-method-from-c/126894/4 "2008-01-24T23:14:33Z")

</div>

This might be a bug - I’ve filed it (#17467).

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Robert  
Brotherus  
Sent: Thursday, January 24, 2008 12:47 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Accessing IronRuby method from C#

Thanks John for your reply!

Still not getting my method calls to work from C# with lambda either:

[TestMethod]  
public void AccessMethod3()  
{  
ExecuteString(@"  
$m = lambda do |par|  
puts 'Hello from lambda: ’ + par  
end  
");  
Proc m = (Proc) GetGlobalVar(“m”); // OK  
Operations.Call(m, “abc”); // InvalidCast: Cannot cast Proc to  
BlockParam  
Delegate1 d = Operations.ConvertTo(m); // OK  
d(“abc”); // InvalidCast: Cannot cast Proc to BlockParam  
}

Robert B.  
Software architect  
Napa Ltd  
Tammasaarenkatu 3, Helsinki FI-00180  
P.O.Box 470, Helsinki FI-00181

Tel. +358 9 22 813 1  
Direct. +358 9 22 813 611  
GSM +358 45 11 456 02  
Fax. +358 9 22 813 800

Email: removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[www.napa.fi](http://www.napa.fi)[http://www.napa.fi/](http://www.napa.fi/)

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of John L. (DLR)  
Sent: 23. tammikuuta 2008 16:59  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] Accessing IronRuby method from C#  
Today we don’t support unbound methods yet (what you are trying to do  
via the call to the method function). If you want first class functions,  
Proc.new and lambda work just fine today.

I’ll let Tomas chime in on AccessMethod2().

Thanks,  
-John

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Robert  
Brotherus  
Sent: Tuesday, January 22, 2008 8:20 AM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] Accessing IronRuby method from C#

Greetings to the mighty IronRuby experts from a humble developer! 🙂

I am testing the latest CVS snapshot (v. 73, downloaded an hour ago) of  
IronRuby.

We are evaluating IronRuby for possible usage in WPF project.

First, I want to test a simple case of defining a method in Ruby and  
calling the method later from C#.

Below I copy a test program that tries this in two ways (methods  
AccessMethod1 and AccessMethod2).  
Both attempts fail with exceptions that are written as comments in the  
code.

I understand that the DLR and IronRuby are in a flux at the moment but I  
still suppose there must be some working way to perform such trivial  
interop task…?

Thanks in advance!

Robert B.  
Software architect  
Napa Ltd

* * *

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using Microsoft.Scripting;  
using Microsoft.Scripting.Hosting;  
using Ruby.Runtime;  
using Ruby.Builtins;

namespace IronRubyTests  
{  
class Program  
{  
private IScriptEnvironment \_scriptRuntime;  
public IScriptEnvironment ScriptRuntime { get { return  
this.\_scriptRuntime; } }  
public IScriptEngine RubyEngine { get { return  
ScriptRuntime.GetEngine(typeof(RubyContext)); } }

```
    public ObjectOperations Operations { get { return 

```

RubyEngine.Operations; } }  
public RubyExecutionContext ExecutionContext { get { return  
Ruby.IronRuby.GetExecutionContext(RubyEngine); } }

```
    public IAttributesCollection GlobalItems { get { return 

```

ExecutionContext.GlobalScope.Dict; } }

```
    delegate void TestDelegate();

    static void Main(string[] args)
    {
        new Program().RunAllTests();
    }

    public Program()
    {
        this._scriptRuntime = Ruby.IronRuby.CreateRuntime();
    }

    public void RunAllTests()
    {
        List<TestDelegate> tests = new List<TestDelegate> {
            new TestDelegate(SimpleTests), new 

```

TestDelegate(AccessMethod1), new TestDelegate(AccessMethod2)  
};  
foreach (TestDelegate test in tests)  
{  
Console.WriteLine(“================== EXECUTING: {0}  
====================”, test.Method.Name);  
try { test(); }  
catch (Exception ex) { Console.WriteLine(ex); }  
}  
}

```
    public IScriptScope ExecuteString(string code)
    {
        return ScriptRuntime.ExecuteSourceUnit(
            RubyEngine.CreateScriptSourceFromString(code));
    }

    public void SetGlobalVar(string varName, object value)
    {
        ExecutionContext.GlobalVariables[SymbolTable.StringToId(varName)] 

```

= value;  
}

```
    public object GetGlobalVar(string varName)
    {
        return 

```

ExecutionContext.GlobalVariables[SymbolTable.StringToId(varName)];  
}

```
    public object GetGlobalConst(string name)
    {
        return 

```

GlobalItems.SymbolAttributes[SymbolTable.StringToId(name)];  
}

```
    public void SimpleTests()
    {
        ExecuteString("puts 'RUBY SAYS HELLO'"); // OK
        SetGlobalVar("x", 666); // OK
        ExecuteString("$y=555"); // OK
        ExecuteString("puts \"ruby: $x = #{$x}\""); // OK
    }

    public void AccessMethod1()
    {
        // Following fails with System.MissingMethodException: 

```

undefined local variable or method ‘method’ for main:Object

```
        ExecuteString(@"
            def sayHello
               puts 'Hello from method!'
            end

            $m = method(:sayHello)
        ");

        // Get the method object:
        object m = GetGlobalVar("m");

        // Here call the method, etc.
    }

    public void AccessMethod2()
    {
        ExecuteString(@"
          module MyMethods
            def sayHello
               puts 'Hello from method!'
            end
          end
        ");

        RubyModule myModule = (RubyModule) 

```

GetGlobalConst(“MyMethods”); // OK  
object sayHello = Operations.GetMember(myModule,  
“sayHello”); // Fails at Assertion at  
ActionBinber.UpdateSiteAndExecute()

```
        // Here call the method, etc.
    }

} // class

```

} // namespace
