views:

161

answers:

1

IronRuby and VS2010 noob question:

I'm trying to do a spike to test the feasibility of interop between a C# project and an existing RubyGem rather than re-invent that particular wheel in .net. I've downloaded and installed IronRuby and the RubyGems package, as well as the gem I'd ultimately like to use.

Running .rb files or working in the iirb Ruby console is without problems. I can load the both the RubyGems package, and the gem itself and use it, so, at least for that use case, my environment is set up correctly.

However, when I try to do the same sort of thing from within a C# (4.0) console app, it complains about the very first line:

require 'RubyGems'

With the error:

 no such file to load -- rubygems

My Console app looks like this:

using System;
using IronRuby;
namespace RubyInteropSpike
{
    class Program
    {
        static void Main(string[] args)
        {
            var runtime = Ruby.CreateRuntime();

            var scope = runtime.ExecuteFile("test.rb");
            Console.ReadKey();
        }
    }
}

Removing the dependencies and just doing some basic self-contained Ruby stuff works fine, but including any kind of 'requires' statement seems to cause it to fail.

I'm hoping that I just need to pass some additional information (paths, etc) to the ruby runtime when I create it, and really hoping that this isn't some kind of limitation, because that would make me sad.

+2  A: 

Short answer: Yes, this will work how you want it to.
You need to use the engine's SetSearchPaths method to do what you wish.

A more complete example
(Assumes you loaded your IronRuby to C:\IronRubyRC2 as the root install dir)

    var engine = IronRuby.Ruby.CreateEngine();

    engine.SetSearchPaths(new[] {
        @"C:\IronRubyRC2\Lib\ironruby",
        @"C:\IronRubyRC2\Lib\ruby\1.8",
        @"C:\IronRubyRC2\Lib\ruby\site_ruby\1.8"
    });

    engine.Execute("require 'rubygems'"); // without SetSearchPaths, you get a LoadError
    /*
    engine.Execute("require 'restclient'"); // install through igem, then check with igem list
    engine.Execute("puts RestClient.get('http://localhost/').body");
    */
    Console.ReadKey();

Kevin Radcliffe
Awesome. I was hoping it was something like that.
Jason Diller
Great! Glad it worked out for you. If you get a chance, please sign up for the IronRuby mailing list, there are a lot of really smart and helpful people there: http://rubyforge.org/mailman/listinfo/ironruby-core
Kevin Radcliffe