Trying IronRuby for the first time

Any developer who is interested in creating an IronRuby project should check out this walkthrough from Justin James of his first experience with IronRuby.

I've had my eye on the IronRuby project for quite some time; unfortunately, it felt like a proper release was looking like vaporware.

When the version 1.0 release of IronRuby hit, it was a bit disappointing that it did not include Visual Studio support, which is more or less required in the .NET ecosystem. By the time IronRuby 1.1 came along, complete with the needed Visual Studio support, it seems like the enthusiasm for IronRuby had disappeared. To get an idea of how long the wait was for it (and how great my excitement was for it), I brought a Ruby book with me on my honeymoon in anticipation of IronRuby's release in December... of 2008. Yes, there was a time where many in the .NET community had an almost palpable hunger to work with IronRuby.

Now that IronRuby is here in a useable format, what is it like? I've had IronRuby installed for some time now, but I did not really know what to do with it. Then I said to myself, "why not try out the stuff in Chad Perrin's Ruby tutorial from September 2010?" So I did, and now I'll walk you through my experience of creating an IronRuby project (which is like any other Visual Studio project).

How to create an IronRuby project

Figure A shows your choices when you're creating an IronRuby project. I think the Silverlight application is an interesting option, and I will be trying this on Windows Phone 7 very soon. I was not sure what Sinatra was, so I looked it up and learned it is an interesting framework for building Web applications. After glancing at Sinatra, I feel like it is potentially useful for rapidly building REST-style Web Services, and possibly other things as well, but I haven't looked too deeply at its documentation yet. I went ahead and created a Console Application to work with Chad's tutorial. Figure A

Your choices when creating an IronRuby project. (Click the image to enlarge.)
Out of the box (Figure B) it had a "hello world" script loaded that looked nearly identical to Chad's first sample. F5 runs it, as expected. As an experiment, I added the "shebang line" (#!/usr/bin/env ruby), which tells a *Nix shell what to use to interpret a script like this, so you can run the script as if it was an application from the shell. Adding the shebang line did not affect how IronRuby interpreted the application, as expected. Figure B

A new IronRuby project. (Click the image to enlarge.)

The next example uses a command line parameter for the "world" part of the output:

#!/usr/bin/env ruby
puts "Hello, #{ARGV[0]}!"

One slight difference between this example and what the project started with is the type of quote. This example uses double quotes, while the default used single quotes (apostrophes). In Ruby, the single quotes indicate that the string should be taken literally, while double quotes allow interpolation to occur. Perl does the same thing, so I was familiar with it.

As you can see in Figure C, Visual Studio gives the embedded variable special treatment when used in a double quoted string. If I were to use single quotes, Visual Studio would display the entire string in maroon, and the interpreter would treat "#{ARGV[0]}" as a literal instead of interpolating the first item in ARGV (the command line parameters). You can open the project's properties to edit these command line parameters (Figure D). Figure C

Double quoted strings get some special treatment. (Click the image to enlarge.)
Figure D

Editing the command line parameters in the project's properties. (Click the image to enlarge.)

In the fourth example in Chad's Ruby tutorial, he introduces us to defining functions. Visual Studio color codes the "def," "end," and "return" keywords in blue just like it does for VB.NET and C#; cues like this help ease the transition for those coming from either of those languages into IronRuby. At this stage, I noted a lack of IntelliSense, at least for the IronRuby keywords. I am not sure if IntelliSense is also lacking for CLR objects -- I haven't gotten to the point when I am using them yet. The final example in Chad's tutorial went without incident.

Figure E

Functions and keywords get some special treatment as well. (Click the image to enlarge.)
Aside from the basics of Chad's tutorial, the IronRuby tools include a real-time interpreter (aka a REPL), and you can execute your application within the REPL as well, which is useful for testing. When I tried out the debugger, it showed the wide range of default variables that Ruby exposes to your application in the Locals window (Figure F). While the toolset is not as in depth as you would find in a dedicated Ruby IDE such as Embarcadero 3rdRail or JetBrains Ruby Mine, it is definitely a step up from a plain text editor. Figure F

The Locals window shows all of the in-scope variables that Ruby has created. (Click the image to enlarge.)


My interest in IronRuby waned because there wasn't a complete product. Now that there is an IronRuby release available that is usable within the Visual Studio environment, I think the door is open enough for anyone to step through if they want.

In the near future, I am going to take a shot at using IronRuby for Windows Phone 7 development. Wish me luck!


Disclosure of Justin's industry affiliations: Justin James has a contract with Spiceworks to write product buying guides; he has a contract with OpenAmplify, which is owned by Hapax, to write a series of blogs, tutorials, and articles; and he has a contract with OutSystems to write articles, sample code, etc.