views:

2174

answers:

7

I am interested in using/learning RoR in a project where I have to use a .NET dll. Is Ruby capable of importing a .NET dll?

A: 

If you use IronRuby (Ruby implementation built on .Net) then it should be able to. If you're already using .Net and want to try out Ruby then you might want to look into IronRuby.

Outside of IronRuby I'm not sure. I haven't used Ruby myself, so I don't know what kinds of things it's capable of.

Herms
+2  A: 

If you want to use 'normal' ruby (since I dont think IronRuby fully runs RoR yet) you might be able to go via COM - ie

"Your Ruby Code" -> RubyCOM -> "COM-Callable Wrappers" -> "Your .NET objects"

RubyCom

That's a bit convoluted though.

Edit: better COM-based option elsewhere in the answers

mackenir
+1  A: 

Another thing - it might be better to think about it more from a Service-Oriented Architecture point. Can you take that .NET DLL and expose it as a service?

Behind the scenes, you can write Ruby modules in C, so you can always write an interop to do what you need. It will limit your deployment to Windows platforms, though (I haven't tried Ruby->Interop->Mono)

Here's a presentation I gave a couple of years back called Ruby for C# Developers. It's a little dated (It was before John Lam's project got folded into IronRuby), but might help a little.

Cory Foy
Expose the .NET dll as a SOA service is a way to go, but i'll have to check the security issues of doing this.
Rafael
+10  A: 

While IronRuby will make short work of talking to your .NET dll (it'll be literally no code), it still is nowhere near running a full rails stack. It's still really slow, as they're still in the "make it work" stage, not the "make it work fast" one. There's also no supported database interaction code yet.

It is moving quickly, but I'd wait probably another year before looking at it for anything serious.

Regarding the COM solution, this may actually be a good way to go.

You don't need the RubyCOM library - that lets other COM objects call into ruby code. To load COM objects from ruby, you just need the win32ole library, which comes as part of the standard library on windows ruby.

Whether or not you can load the dll from COM will depend if the .NET dll was built to be 'Com Visible'. The .NET framework defines a ComVisibleAttribute, which can be applied to either an entire assembly, or specific classes within an assembly. If it is set to true for either the whole assembly, or any classes, then the dll will already be callable from COM without any wrapper code.

Here's a test I did.

Create a new .NET dll project (class library). Here's an example class I used:

using System;
using System.IO;

namespace ComLib
{
 public class LogWriter
 {
  public void WriteLine( string line )
  {
   using( var log = new StreamWriter( File.OpenWrite( @"c:\log.file" ) ) )
   {
    log.WriteLine( line );
   }
  }
 }
}

Now, under the visual studio project, there is a directory called Properties which contains AssemblyInfo.cs. In this file, there will be the following

[assembly: ComVisible( false )]

Change the false to true. If you don't want every class in the assembly exposed to COM, then you can leave it set to false in AssemblyInfo.cs and instead put it above each class you want to expose, like this:

[ComVisible( true )]
public class LogWriter ....

Now right click on the dll project itself, and from the popup menu, select 'properties'. In the list of sections, choose Build

Scroll down, and tick the 'Register for COM interop' checkbox. Now when you compile this DLL, visual studio will do the neccessary stuff to load the COM information into the registry. Note if you're on vista you need to run VS as an administrator for this to work.

Now that this is done, recompile your dll, and then create a new ruby file.

In this ruby file, do this:

require 'win32ole'

lib = WIN32OLE.new('ComLib.LogWriter')
lib.WriteLine('calling .net from ruby via COM, hooray!')

Ruby that ruby file, and presto! you should see that the text gets written to c:\log.file.

One problem with this solution is that it requires that the .NET dll is already Com Visible, or if it's not, you have the ability to recompile it. If neither of these things are true, then you may have to look at other options.

Good luck!

Orion Edwards
Thanks! I'll check the comVisible property of dll to see if I can implement this solution.
Rafael
A: 

Thanks to all!! I will study the solutions and try with the .NET dll.

Rafael
A: 

Although it's a dead project AFAIK, you may find the previous RubyCLR project by John Lam (who is now in charge of IronRuby) an interesting one.

Thibaut Barrère
A: 

If you are interested in ASP.NET MVC with IronRuby together, You may find yourself to check out this source code from Jimmy - http://github.com/jschementi/ironrubymvc/tree/master

Enjoy!

Jirapong