RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Ruby Comes to the .NET Platform

Find out why .NET programmers may want to learn and use Ruby, and discover the core syntax of the language.

icrosoft's IronRuby project brings a powerful and fun dynamic language to the Windows platform. The Ruby programming language is a modern, object-oriented scripting language, with a syntax inspired by languages such as Perl and Smalltalk. It was conceived by Yukihiro Matsumoto (aka "Matz"). In his words, Matz wanted a language that was "more powerful than Perl and more object-oriented than Python" (read more from Matz). The language is designed to feel natural; something Matz calls the "principle of least surprise." Version 1.0 of the language was released in 1996.

For several years, Ruby existed as a little-known scripting language that excelled in the tasks for which it was originally designed: manipulating data and environment with the least amount of effort. When I first started toying with it a few years ago, I was looking for something to replace batch files for automating some of my more common administrative tasks.

Ruby's popularity really began to pick up when a small software company from Chicago, Illinois, named 37signals released a Web application framework named Rails. This new framework combined proven patterns like Model-View-Controller and ActiveRecord with new ideas like "convention over configuration." The end result is a framework that achieves a lot with almost no code.

RubyCLR and IronRuby
In early 2006, John Lam went public with a project named RubyCLR, which worked as a bridge between Ruby and .NET. It allowed the user to have direct access to the richness of the .NET platform from Ruby, and even expose Ruby objects to the CLR. This project was very ambitious, but it wasn't about putting Ruby on .NET so much as it was about letting the two worlds talk to each other. You still needed the Ruby runtime to be installed on your machine.

The RubyCLR project was a tremendous first step toward understanding how to get Ruby and .NET to live together in harmony. John's work did not go unnoticed, and at the end of 2006, he announced on his weblog that he would be joining Microsoft's newly formed Dynamic Language Runtime (DLR) team. Just a few months before John's announcement, Microsoft released version 1.0 of IronPython, a new implementation of the Python language on the .NET Framework. The Dynamic Language Runtime work takes the work done on IronPython and builds a runtime on top of the .NET Framework that allows dynamic languages to be brought into .NET with much less effort.

John and his team announced IronRuby at the MIX conference in 2007. Perhaps more surprising than the revelation of the IronRuby project itself was that it would be the first truly open source .NET language from Microsoft. Not only would the source code be available, but outside contributions from the community are also accepted.

IronRuby is still a work in progress. Although drops have occasionally been available, usually in the context of another project (such as the recently released Silverlight 2.0 Beta 2), those following the project are keeping up with the source tree and participating in the mailing lists.

Why Learn Ruby?
One of my favorite books, The Pragmatic Programmer: From Journeyman to Master (Andrew Hunt and David Thomas, Addison-Wesley Professional), encourages you to learn a new language every year. For me, the language that most changed my world-view when I learned it was Ruby.

Ruby is a fully object-oriented language in the Smalltalk sense. That means everything you interact with in the system is an object, including direct values like numbers. Even classes, which form the templates from which new object instances are created, are themselves objects in the system.

Because Ruby is a dynamic language, you'll find that types become much less important. When a method takes an object as a parameter, you don't need to specify what type that object needs to be. In fact, there is no compiler, so you might not discover until runtime that the object you passed to a method wasn't suitable for the requirements of that method.

If you're like I was a few years ago, you might find this concept a little unsettling. Aren't compilers here to provide help to you, so you can know these things as soon as possible before they cause runtime errors? The answer is, of course, yes: compilers do provide better feedback, sooner. So why should you throw them away and use a language like Ruby?

As it turns out, compilers are a very seductive form of straitjacket. The ability of compilers to tell you about mismatched types necessarily causes a form of type-rigidity. When you write a method, you may wish to say "objects here must be able to do foo and bar" and then invent an interface called IFooBar. That seems like a neat solution for describing the requirements—but it fails you when you want to use someone else's classes (especially those from the framework) that were written before IFooBar existed.

Author's Note: Although IronRuby isn't quite ready for mainstream use, you can use the standard version of Ruby for your explorations. If you want to follow along with the examples, you can download the Ruby installer for Windows.

Ruby by Example
The best way to learn a language like Ruby is by exploring it interactively. Many dynamic languages have interactive prompts known as a Read-Execute-Print Loop (REPL). The REPL program for Ruby is called irb (which standards for "interactive Ruby").

When you execute the irb program, you'll see an irb prompt:

   C:\Users\Brad> irb
You type commands into the irb prompt and the Ruby interpreter evaluates them, with the results printed to your screen. REPLs like irb are an excellent way to learn a language: one statement at a time.

As an introduction to irb, tell Ruby to add two numbers. At the irb prompt, type 5 + 2 and press Enter:

   irb(main):001:0> 5 + 2 
   => 7
The irb(main):001:0> part is the prompt that irb gave you. When you typed 5 + 2 and pressed Enter, irb printed the result to you as => 7. The => is your hint that irb is showing you the result of the evaluation.

If Ruby believes you haven't finished your expression, it will allow you to continue. For example, if you were to type 5 + 2 + and press enter, Ruby realizes that you're still missing part of the expression and lets you continue typing on the next line:

   irb(main):002:0> 5 + 2 +
   irb(main):003:0* 13
   => 20
The second prompt line ends with "*" instead of ">" to let you know that you're still finishing a previous statement.

Editor's Note: This article was first published in the September/October 2008 issue of CoDe Magazine, and is reprinted here by permission.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date