Software Development

Four IronRuby and .NET interop choices

How can developers get their IronRuby code to talk to the rest of their .NET code? Justin James presents four interoperability options.

The only situations that you would use IronRuby without interacting with the .NET environment are when you can load .NET assemblies but not a Ruby interpreter (for instance, a Windows Phone 7 application). IronRuby does not bring anything to the table that a standard Ruby environment does not except for .NET interoperability and, in many regards, IronRuby is a bit lacking. Here are four ways that we can get our IronRuby code talking to the rest of our .NET code.

1: Run IronRuby code within .NET

I recently wrote about using IronRuby from within a Windows Phone 7 application. In that application, .NET code loaded the IronRuby runtime, established the environment (including global variables that represented the input), ran the code, and examined the results. The information in that article will work on any .NET project. This method is best if you have a small piece of functionality that you would like to run in Ruby within a larger project. By keeping your input and output to the script basic (simple types such as strings, numbers, and Booleans), you minimize casting back and forth between the two environments.

2: Call IronRuby code within .NET

This is similar to option #1, but instead of running a Ruby script within your .NET application and scanning the results, you can use Ruby to create various classes, and then call those classes from your .NET code. If you want to write large pieces of functionality in Ruby but keep the overall project in .NET, this is your best bet. The downside is that Ruby's dynamic nature means that you need to use dynamic objects on the .NET of the equation, which means no IntelliSense. This is still a great way to get things done.

To get the IronRuby code available to the .NET application, you can either execute the IronRuby code file as explained in my Windows Phone 7 blog post or include it as a string and call the execute on the string.

Code sample A: Embedding IronRuby in C# code
var rubyRuntime = IronRuby.Ruby.CreateRuntime();
var rubyEngine = rubyRuntime.GetEngine("rb");
rubyEngine.Execute(@"
class IronRubyTest
def helloworld
puts 'Hello world!'
end
end
");
object testClass = rubyEngine.Runtime.Globals.GetVariable("IronRubyTest");
dynamic tester = rubyEngine.Operations.CreateInstance(testClass);
tester.helloworld();
Console.ReadKey();

This is a good amount of code to build out a class and run it, but for the cases where working with Ruby makes sense, this code is a small price to pay for its advantages.

#3: Call .NET code within IronRuby

If you want application to be in Ruby, but you have some vital .NET code you need to access, no worries! IronRuby is able to load .NET assemblies quite easily, and then call into them to create objects and call methods. You should make sure that you add the directory that contains the DLL with the .NET assembly to the "Search Path" in Visual Studio (in the IronRuby project tree). Here is a simple .NET class that we'd like to call from IronRuby.

Code sample B: .NET code to call from IronRuby
using System;
namespace DotNetTestLibrary
{
public class DotNetTestClass
{
public string Property1 { get; set; }
public string CreateHelloWorldText(string helloObject)
{
return "Hello " + helloObject;
}
}
}
Code sample C: Calling the .NET class from IronRuby
require 'DotNetTestLibrary'
TestClass = DotNetTestLibrary::DotNetTestClass
tester = TestClass.new
tester.Property1 = "World"
puts tester.CreateHelloWorldText(tester.Property1)
gets

4: Web services

It can be easy to overlook Web services. Windows Communication Foundation (WCF) makes it really easy to expose our .NET code as a Web service. You could have your WCF service expose SOAP, RSS, JSON, or XML to be used by the IronRuby application; you could even leverage the .NET libraries for working with WCF within IronRuby. And of course, .NET can consume Web services; it isn't hard to go from having a Ruby application to creating a Web service, especially since IronRuby supports Rails.

J.Ja

About

Justin James is the Lead Architect for Conigent.

0 comments

Editor's Picks