Archives page

Posts Tagged ‘dotnet’

//TODO: Feature Release for v2012

I did one of these back in 2009 and after 2 years, I can successfully say I’ve accomplished… some of them.  It’s the sort of list I’d make an annual review if they didn’t have to follow fluffy company ideals and goals.  Not a new years resolution as such – but technologies and platforms I want to be looking at in the near future. »

Running Ruby methods within C# / .NET

The last example might have been a little too trivial, even by my standards. Even I struggled to imagine a scenario where I might ever need to use it. So hopefully this one will be a little bit more interesting and demonstrate something more useful.

Useful, but still just as simple as the previous examples, that is. Again – you’ll need your references from the downloaded IronRuby bin/ folder. And as you’ve come to expect, a very simple ruby script defining a lambda function.

Code block    
$m = lambda {
            a = Array.new
            a.push(2, 3)
            (4..50).each do
               |i|
               (2..(Math.sqrt(i).ceil)).each do
                  |thing|
                  if (i.divmod(thing)[1] == 0)
                     a.push i
                     break
                  end
               end
            end
            return a
         }

From this, we’ll get an array of the prime numbers. The function can then be executed rather nicely from within your .NET code like this:

Code block    
var ruby = Ruby.GetEngine(Ruby.CreateRuntime());
ruby.Execute(@"
    $m = lambda {
//.. snip..
            return a
         }
");
var rubyContext = Ruby.GetExecutionContext(ruby);
var m = (Proc)rubyContext.GetGlobalVariable("m");
var rubyArray = (RubyArray) m.Call();
 
foreach (var o in rubyArray)
{
    Console.Write(string.Format("{0},", o));
}

Now we’re really starting to leverage that syntactical beauty of ruby within .NET and jumping (almost) seamlessly between the two. Now, I really should do some demos on something more useful than prime numbers, and perhaps get into one of the big areas of interest of Ruby – testing frameworks. Not tonight though 🙂

Demo project available as usual:

Executing complete (Iron)Ruby scripts from within native C# / .NET

Running Ruby code as-is within .NET is almost too simple to even write home about. In fact I almost didn’t, but after I wrote it the solution stared up at me with its big brown eyes, and I couldn’t resist. I’m not totally sure when I’d ever need to use this, but perhaps taking some legacy Ruby scripts where I don’t really care about the results, and still want to run them amongst some other .NET tasks. It might come up in some obscure unit testing one day. You never know.

So, this still serves as an example of how simple it can be to run ruby scripts within .NET. We’ll get to making use of some return values, and running individual methods in the next post.

Following on from the last example, the following ruby script prints all the prime numbers between 1 and 50.

Code block    
state = Numeric.new
print "2,3,"
(4..50).each do
   |i|
   (2..(Math.sqrt(i).ceil)).each do
      |thing|
      state = 1
      if (i.divmod(thing)[1] == 0)
         state = 0
         break
      end
   end
   print "#{i}\," unless (state == 0)
end

With the ruby script complete (you can also run this with the ir.exe that ships with IronRuby if you like)… Next start up a new project within Visual Studio, and add some references from your downloaded IronRuby bin folder (IronRuby.dll, IronRuby.Libraries.dll etc…).

You only need a few lines of code to execute your ruby script.

Code block    
using IronRuby;
// ..
var runtime = Ruby.CreateRuntime();
runtime.ExecuteFile(@"ruby/run.rb");

As before, you can download the full example to check it and run for yourself. And also as before, you will also need IronRuby.