IronShay

Ironing code, geek t-shirts and even presentations!

NAVIGATION - SEARCH

Starting Using IronRuby in 25 Minutes

On the journey to cross the language barrier that stands between IronRuby and .NET developers, this post will teach you everything you need to know to start using IronRuby. And the intention is that it will not take you more than 25 minutes. So go grab a coffee and start reading.

….

I’m waiting… go get that coffee already!

….
………
…………

Good! Now that we’re ready to go, let’s start!

So, first task for you, which will take the first 15 minutes of our time is to learn Ruby. This is exactly the reason for my latest post – Learning Ruby in 15 Minutes. Go ahead and read it and then come back here.

……………

OK. Now that you know Ruby and we still have 10 minutes left to learn IronRuby. Not a lot of time but hey, that’s all we need.

First of all – Download and Install IronRuby

Well, to use IronRuby you must first download and install it. So if you have .NET 4 installed, download the IronRuby installer from here. Otherwise, download this version which supports .NET 2.0 SP1 and above. This will download an MSI file, so to install IronRuby just run the installer, then click Next, Next, Next and Finish. That’s it, you’re done.

Pay attention that IronRuby supports all the Ruby language features. Therefore, everything you’ve learned on the 15 minute Ruby course apply to IronRuby as well.

Running IronRuby Files

When running IronRuby files, the important thing to have is an IronRuby file… Ruby code files are usually saved with the .rb extension. Hence, after you have written your precious Ruby code, save it to a file with the .rb extension. For example, let’s say we save the next code in a file named hello.rb:

# Class definition
class Hello
  def say_hello
    puts "Hello!"
  end
end

# The code that will get executed
hello = Hello.new
hello.say_hello

To run this file you will need to open the command prompt. This is done in Windows via the Start->Run dialog (shortcut: WinKey+R) – input cmd in this dialog and the command prompt will open. As soon as you have the cmd window opened, you can run IronRuby files.

Running IronRuby files is done using the IronRuby interpreter which exists within the ir.exe file. Therefore, the command to run IronRuby files starts with ir.exe followed by the file name. If the run file is placed within the current directory you only need to specify the name of the file. If it is located in a different folder, you need to specify the entire path. For example, the next cmd window screenshot shows running the hello.rb file from the previous sample:

Running IronRuby Files

Referencing .NET Assemblies

The expertise of IronRuby is its integration with .NET assemblies - you can load any .NET assembly and use it from Ruby code.

Doing so is just like loading other Ruby files – via the require method (which was mentioned on the Learning Ruby in 15 minutes post). All you need to do is to pass the name of the assembly (with or without the full path) to the require method:

require 'MyCustomAssembly.dll'
require 'C:\Demo\SomeAssembly.dll'

In case the assembly is located in the GAC or is signed, pass its strong name:

require "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 

If you want to learn more about referencing .NET assemblies, look here.

Using .NET Assemblies

After you load .NET assemblies using the require method (or the other alternatives), you can use everything inside these assemblies. For this you need to be aware of a few things:

  • The namespace delimiter in Ruby is ::. Therefore, System.Windows.Forms will be System::Windows::Forms in IronRuby.
  • The method include is similar to C#’s using keyword so you can take advantage of it. For example: include System::Windows::Forms.
  • When a .NET assembly is loaded into IronRuby context it goes through a name mangling process. This process converts the names of the .NET properties and methods to their equivalent Ruby name (because Ruby has different naming conventions). You will still be able to use the C# name, but it is important to be familiar with that.
    The name mangling process converts the .NET’s CamelCase convention to Ruby’s lowercase_and_delimited convention. Read more about this process here.

The next sample demonstrates using the System.Windows.Forms assembly to create a simple form with a label via IronRuby. Pay attention to the names of the methods and properties which are the results of the name mangling process:

require "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 

include System::Windows::Forms

# Create a form object
form = Form.new

# Create a label
label = Label.new
label.auto_size = true

# Set the text of the label
label.text = "IronRuby Rulezzzzz!!!"

# Add the label to the form
form.controls.add label

# Show the form
form.show_dialog

Conclusion

You have successfully crossed the language barrier! Congratulations!

This post and the one before it taught you all the basic stuff. Even though it is enough to get started, I think your experience with Ruby and IronRuby will be much more fulfilling after you learn more advanced subjects. There are numerous sites, tutorials and books ( *caugh* IronRuby Unleashed *caugh*) you can read and a great deal of forums and mailing lists where you can ask questions. So don’t be afraid to start, we’re all here to help!

Enjoy!
Shay.

kick it on DotNetKicks.com Shout it



Pingbacks and trackbacks (5)+

Add comment