Geeks With Blogs
Greg Malcolm Everything but the Perl
<< Part 1 - Installing IronRuby and Cucumber

A quick look at IronRuby


IronRuby is an implementation of Ruby which runs on .NETs Dynamic Language Runtime platform (the DLR). Languages hosted on the DLR are interpreted at runtime, rather than compiled then executed (like C# applications).

One benefit is that code can be invoked on the fly from a command line shell. This is handy when debugging or figuring out how to do something. Other popular DLR hosted languages include IronPython and Silverlight.

DLR Diagram

Messing around with Interactive IronRuby Shell (IIRB)

In standard ruby the interactive shell is invoked by running “IRB”. With IronRuby we prefix it with an extra “I", giving us “IIRB”.

Let's try that. From a windows command prompt type iirb.bat<enter>:

IIRB Prompt Screenshot 

This looks exactly like the standard Ruby IRB, but the unfortunate delay on loading is a giveaway. Hopefully that delay will reduce as the framework matures!

First off, lets confirm that it is a Ruby interpreter. Try making it interpret some basic ruby expressions:

"live racecar !"
"live racecar !".reverse
"live racecar !".reverse * 5
("live racecar !".reverse * 5).split(" racecar ")
("live racecar !".reverse * 5).split(" racecar ").map {|x| result = "We" + x}

These are the result I got:
IIRB showing ruby commands executed screenshot
Yep, it looks just like conventional Ruby so far! Of course there is a lot more to Ruby, but we’re not going to cover it here. Try these resources instead:

So, how about something you can’t do in standard Ruby? How about we use IIRB to show a .NET MessageBox?

Showing .NET MessageBoxes

First we need access to the .NET libraries. For accessing a message box in .NET app we would need to add a reference to a library like ‘PresentationFramework.dll’.

In IronRuby we do this through the require keyword. Excute this from the IIRB:

require 'PresentationFramework'

Now we can have access to the message box. Try this next:

System::Windows::MessageBox.Show("Mein fruhstuck ist sehr langweilig!",
  "IronRuby Demo")

You should now see this:
MessageBox screenshot

This is almost the same as using the MessageBox object from C#, except that we are using “::” instead of “.” to separate namespaces and there is no semicolon at the end.

Personally I don’t want to keep referring to MessageBox namespace every single time I use it. Let's create another
MessageBox object with a shorter name:

MessageBox = System::Windows::MessageBox 'Mein fruhstuck ist noch immer sehr langweilig!',
  'IronRuby Demo'

You may notice that I also switched over to using the Ruby style lowercase version of “Show()” method and that I dropped the parenthesis. I also switched to apostrophes instead of quotes. This is just another way of doing the same thing in Rubyland.

So... we can now can show a message box. Big deal! How about we modify the class at runtime? Ruby has this neat mixins feature which allows classes to be modified at any time. Here goes:

class MessageBox
  def self.wide_show(msg) msg, 'This message box is ' + ('very ' * 8) +
      ' wide, is it not?'


MessageBox.wide_show 'Testing 123...'

… and we now have a customized “extra wide” MessageBox at our disposal
Wide Messagebox Screenshot

As you may have already figured out, we extended the MessageBox class, “def” defines a function, and the putting the “self.” prefix is similar to using “static” or “shared” in .NET. It makes it a “class method”


Quit out of irrb (type “quit:), start it up again afresh (“iirb”). Copy and paste the following:

require 'PresentationFramework, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
require 'PresentationCore, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

require 'PresentationFramework, Version=, Culture=neutral,
require 'PresentationCore, Version=, Culture=neutral,

Windows = System::Windows

Media = Windows::Media

redBrush = Media::Colors.Red )
blackBrush = Media::Colors.Black )

redButton =
redButton.content = "Do NOT press!"
redButton.font_size = 40
redButton.font_weight = Windows::FontWeights.bold
redButton.Width = 600
redButton.Height = 400
redButton.background = redBrush do |sender, args| 'Please do not press this button again!',
    'Why would you even do that?!?',

    Windows::MessageBoxButton.OK, Windows::MessageBoxImage.exclamation

warningWindow =
warningWindow.content = redButton
warningWindow.title = "Example WPF Window"
warningWindow.background = blackBrush

app = warningWindow

Yep, this demonstrates how you can create a WPF window on the fly:

WPF Screenshot

One last thing for this segment, the DLR is designed so that programs hosted on it can interact with each other regardless of which languages the different parts are written in. There is a rather cool Silverlight sample out there called “DLRConsole” which demonstates how you can modify Silverlight elements from IronRuby or IronPython in the same browser window.

This is the URL:

Note: it could move at any time without warning. Just google on “DLRConsole” if this happens.

DLR Console

In Part 3 we will have at a partical use for IronRuby, BDD with Cucumber
Posted on Thursday, November 5, 2009 8:30 PM | Back to top

Comments on this post: IronRuby and Cucumber - IIRB Tutorial (Part 2 of 3)

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Greg Malcolm | Powered by: