<< Part 1 - Installing IronRuby and Cucumber
A quick look at IronRuby
Background
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.
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>:
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:
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:
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
MessageBox.show '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)
MessageBox.show msg, 'This message box is ' + ('very ' * 8) +
' wide, is it not?'
end
end
MessageBox.wide_show 'Testing 123...'
… and we now have a customized “extra wide” MessageBox at our disposal
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”
WPF
Quit out of irrb (type “quit:), start it up again afresh (“iirb”). Copy and paste the following:
require 'PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
require 'PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
require 'PresentationFramework, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'
require 'PresentationCore, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'
Windows = System::Windows
Media = Windows::Media
redBrush = Media::SolidColorBrush.new( Media::Colors.Red )
blackBrush = Media::SolidColorBrush.new( Media::Colors.Black )
redButton = Windows::Controls::Button.new
redButton.content = "Do NOT press!"
redButton.font_size = 40
redButton.font_weight = Windows::FontWeights.bold
redButton.Width = 600
redButton.Height = 400
redButton.background = redBrush
redButton.click do |sender, args|
Windows::MessageBox.show 'Please do not press this button again!',
'Why would you even do that?!?',
Windows::MessageBoxButton.OK, Windows::MessageBoxImage.exclamation
end
warningWindow = Windows::Window.new
warningWindow.content = redButton
warningWindow.title = "Example WPF Window"
warningWindow.background = blackBrush
app = Windows::Application.new
app.run warningWindow
Yep, this demonstrates how you can create a WPF window on the fly:
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:
http://silverlight.net/content/samples/sl2/dlrconsole/index.html
Note: it could move at any time without warning. Just google on “DLRConsole” if this happens.
In Part 3 we will have at a partical use for IronRuby, BDD with Cucumber