In this article we will demonstrate how to download IronRuby using Github and build IronRuby using VS 2008, VS 2010 Beta 1. We will also discuss how to interact with the .NET framework using IronRuby and how to use IronRuby from the C# language.

Introduction:

In this article we will demonstrate how to download IronRuby using Github and build IronRuby using VS 2008, VS 2010 Beta 1. We will also discuss how to interact with the .NET framework using IronRuby and how to use IronRuby from the C# language.

Why IronRuby?

Why indeed! IronRuby is the .NET implementation of the Ruby language which runs on top of the DLR (Dynamic Language Runtime). If you have ever programed in the Ruby language then you must already know that Ruby is a beautiful language. The simplicity of the Ruby language appeal to most developers in a pleasing way. These are some good reasons to jump on the IronRuby bandwagon.

Downloading IronRuby:

The IronRuby project has been moved to GitHub. In this article we will be using an application called msysgit to download the source code for IronRuby. Msysgit will allow us to communicate with Github and download the project on our machine. Go to the following link and download msysgit application.

http://code.google.com/p/msysgit/downloads/list 

After downloading msysgit simply install the application on your machine. After installing when you right click on your folder you will get a new option related to Git as shown below:



We will use the "Git Bash Here" option which will give us a command line tool to download the project from Github. Create a new folder called IronRuby and right click on it and select "Git Bash Here". You will get the following command window.



Now, we will use the command line options to download IronRuby in our C:\IronRuby folder. Check out the screenshot below which uses the command to download a copy of IronRuby from the Github repository.



Building the IronRuby project:

There are several ways to build the IronRuby project. You can use rake compile option or you can build the Visual Studio 2008 Ruby project. We will perform the later one. Browse to the Ruby folder to find the Ruby Visual Studio solution. Check out the screenshot below which also shows the path where the solution exists.



Open the solution and build the project. Now, let's see how to interact with IronRuby using the IronRuby console (IR.exe).

Working in IR:

First task is to locate IR.exe file in your IronRuby folder. We searched for the ir.exe file in the IronRuby folder and came up with the following result:



You can go to the C:\IronRuby\IronRuby\Merlin\Main\bin\Debug folder and run IR.exe or you can place IR.exe and all of its dependents into the C:\Windows\System32 folder. You can find all the dependencies by placing the IR.exe into the System32 folder and then running ir.exe from the command line. This will result in errors. The errors will clearly state what dependencies (dll) files are missing. Simply, copy the missing dll files into the System32 folder and you are good to go.

Now, run the ir.exe file from command prompt and you will see the following screen:



This is the IronRuby interactive console window. The good thing about IR is that every line you type is ran and the result is outputted on the screen immediately. Try it out yourself. Check out the simple example below:



The above screenshot shows some of the basic mathematical operations. Since, IronRuby is build on top of DLR you can also interact with the .NET libraries. Check out the code below which is used to open a new Window.



As, you can see IR console is a powerful way of interacting with IronRuby and getting quick instant feedback. So, we can interact with the .NET libraries using IronRuby but can we interact with IronRuby using our static typed language (C#, VB.NET).

Why Calling IronRuby Methods from a Statically Typed Language?

Just as IronRuby, a DLR language can benefit from the .NET framework. The .NET framework static languages can also benefit from the simplicity of the dynamic languages. There are many scenarios where code written in dynamic language is much simpler, cleaner and concise then written in a static language.

Using IronRuby with Visual Studio.NET 2008:

The first task is to add reference to the required libraries to your project. Hopefully, you have already downloaded IronRuby project on your local machine from GitHub. If you have not then please refer to the "Downloading IronRuby" section at the start of the article.

Here is a screenshot of references required to run IronRuby on VS 2008.



You will find all the libraries in your IronRuby folder. Remember to build the IronRuby project as described at the start of the article.

Once, you have all the references setup you are ready to execute your first IronRuby application using C# code.

Executing IronRuby Code Using C#:

There are couple of steps required to execute IronRuby code from C#. First, you need to setup the run time as shown in the code below:

 

Next, you need to get the IronRuby engine.



Now, you can execute IronRuby code using different methods available through the engine instance:



Here is the complete code:



And here is the output:



That is pretty sweet right! Executing dynamic code from static code has opened a lot of doors for us. This is a whole new world where different cast of languages can live happily and work together to achieve a better software.

You can execute any ruby code using the above method. Take a look at the MyRubyFile.rb file which contains Ruby code:


 
We can execute the above code using the following C# code:



The result of the above code will be the same but this time we are executing a Ruby file instead of executing a Ruby expression.

Invoking Actions on Ruby Objects Through C#:

Now, that you have got your feet wet with C# to IronRuby pipeline we can dive into much interesting stuff. Currently we are executing IronRuby code in an isolation. That isolation is either a string expression or a ruby file which is executed by the IronRuby engine. Let's get the RubyObject into the static world and perform some actions on it.

The Ruby code will stay the same as defined in MyRubyFile.rb file. Here is the C# code:



We use engine.Runtime.Globals.GetVariable("Person") to get the Person variable. Then we use the CreateInstance method to create the instance of that variable. Finally, we use the InvokeMember method to invoke the "foo" method on the person object. The result is shown below:



Using IronRuby with Visual Studio.NET 2010 Beta 1:

You might be wondering why is there a separate section for VS 2010 Beta 1. The reason is the inclusion of the dynamic keyword in the C# language. The dynamic variable type allows to interact with COM objects and Dynamic types in the DLR. Let's see the dynamic variable type in action.

But first you need to download the latest build of IronRuby solution for the VS2010 Beta 1 which contains the support for the dynamic types. You can download the build using the following URL:

IronRuby Project for Visual Studio 2010 Beta 1

Once, you have downloaded the project you can open the solution in VS 2010 and build the solution. Your solution file is located under the following folder.

[Your Download Folder]\ironruby-ctp-dev10-beta1\examples\RubyDynamicInterop

Once, the solution is build you can add the following dlls to your project:

IronRuby.dll
IronRuby.Libraries
Microsoft.Scripting

Here is the Ruby code which will be executed as an expression.



The Person class contains a single method called "greet" which simply puts the "hello world" string on the screen. Following is the C# code which is used to execute the IronRuby code:



As, you can see we are using the power of the dynamic keyword to get a dynamic type object from the DLR world into the C# world. The person.greet() will trigger the greet method defined by the IronRuby Person object.

NOTE: Don't expect to get any intellisense support for the dynamic person class. The compiler cannot generate intellisense for dynamic type objects because of their dynamic behavior.

Screen Scraping Using IronRuby from C#:

Let's move to something interesting. We are going to use IronRuby to scrape a webpage and return the HTML back to C# for display. Here is the Ruby code which is used to scrape the screen:



And here is the C# code:



If you run the above code an error will be thrown saying that "unable to locate the open-uri". This is because "open-uri" namespace is contained in a separate file which must be added to the search paths for the IronRuby engine. Here is the code which adds the path to the IronRuby engine instance:



Now, if you run the code it will work as expected and scrape the passed in URL. Here is the result:



Conclusion:

In this detailed article we learned quite a number of things. We learned how to download and build the IronRuby project. We also demonstrated how to call the .NET libraries using the IronRuby IR (Interactive Ruby) console. Finally, we demonstrated how to call IronRuby methods from the C# language.

[Download VS 2008 Sample]

[Download VS 2010 Beta 1 Sample]

References:

1)
http://msdn.microsoft.com/en-us/magazine/dd434651.aspx
2) http://www.ironruby.net/
3) http://blog.benhall.me.uk/2008/12/downloading-ironruby-from-github.html