Search code examples
c#ironrubydynamic-language-runtime

Problems with including an interface in IronRuby


I have an interface which looks something like:

interface IMyInterface {
    MyObject DoStuff(MyObject o);
}

I want to write the implementation of this interface in IronRuby, and return the object for later use.

But when I try to do something like

var code = @"
    class MyInterfaceImpl
        include IMyInterface

        def DoStuff(o)
            # Do some stuff with o
            return o
        end
    end

    MyInterfaceImpl.new";

Ruby.CreateEngine().Execute<IMyInterface>(code);

I get an error because it can't be cast to IMyInterface. Am I doing it wrong, or isn't it possible to do what I'm trying to do?


Solution

  • It isn't possible to implement a CLR interface in IronRuby and pass it back into CLR. 'MyInterfaceImpl' in your example is a Ruby class, not a CLR realization of 'IMyInterface'.

    I stand corrected, per Jimmy Schementi's post.

    You could however use IronRuby types as dynamic objects inside your .NET code:

    
        var engine = Ruby.CreateRuntime().GetEngine("rb");
        engine.Execute("/*your script goes here*/");
    
        dynamic rubyScope = engine.Runtime.Globals;
        dynamic myImplInstance = rubyScope.MyInterfaceImpl.@new();
    
        var input = //.. your parameter
        var myResult = myImplInstance.DoStuff(input );