So today while at work I needed to create a simple IronRuby prototype to make sure I understood how to host or call IronRuby code from C#.   I know that I have read a number of articles about how easy it is but when "Binging" around for a solution all I returned were a bunch of forum posts on the IronRuby site and no simple snippets that put all of the pieces below.

Using an Interface to make life easier in .Net 3.5

After reading a number of posts and putting some pieces together I decided that what I wanted to do was use a .Net Interface to interact with my IronRuby class that is meant to be as an extension layer to a .Net application that I am building.  Below is the code that demonstrates how to create a hosting engine in C# and call IronRuby methods from C# without using .Net 4.0 as my application is running on .Net 3.5.

using System;
using System.Collections.Generic;
using Microsoft.Scripting.Hosting;
using IronRuby;
 
namespace IronRubyConsole
{
    class Program
    {
        static int Main(string[] args)
        {
            var runtime = Ruby.CreateRuntime();
            var engine = runtime.GetEngine("rb");
            //engine.Execute("puts 'Just a Trial Method'");
            var scope = engine.Execute(@"
            require 'IronRubyConsole'
 
            class ExampleCalc
               include IronRubyConsole::ICalc
             
              def calculate()
                puts 'hello IronRuby from C# code'
                return 0
              end
            end
            return ExampleCalc.new
");
 
            ICalc calculator = (ICalc)scope;
            var result = calculator.calculate();
            return 0;
        }
 
    }
 
    public interface ICalc
    {
        object calculate();
    }
}
Submit this story to DotNetKicksShout it   Bookmark and Share