First Look at IronRuby
Published: 23 Jul 2007
Unedited - Community Contributed
In this article, Scott gives you a sneak preview of IronRuby with the help of a sample application.
by Scott Guthrie
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 30081/ 88


Republished with Permission - Original Article

Over the last few years we've been working to make .NET and the CLR a great environment for dynamic languages.  About 14 months ago we formed a dedicated group within my team that has been focused on adding richer CLR runtime support for dynamic languages, as well as delivering first class .NET implementations of popular dynamic languages.

DLR Background

This spring we shipped the first preview release of a new .NET library that we call the "Dynamic Language Runtime" (or DLR for short).  It provides a set of features on top of the CLR designed explicitly for dynamic language scenarios.  These include a shared dynamic type system, language hosting model, and support to make it possible to generate fast dynamic code.  These features make it much easier to build high-quality dynamic language implementations on .NET.  These implementations can access and use any of the APIs in the .NET Framework, as well as easily interoperate with code written in any other .NET language (for example: you could write a Ruby class that invokes and uses a C# class, which in turn invokes a Python class).

This spring at the MIX 07 conference we announced that Microsoft will be shipping 4 dynamic language implementations of our own for .NET:

·         IronPython

·         IronRuby (new)

·         Javascript

·         Dynamic VB (new)

The source code of our IronPython implementation, as well as the source code for the underlying DLR library, was published on CodePlex in April.  You can download both of them today from the IronPython codeplex site.  All of the source is made available under the MSPL permissive license - which provides full commercial and non-commercial modification rights.

IronRuby Pre-Alpha Release

Today we are making available the first public drop of our IronRuby implementation.  You can learn more about how to download the source, build it, and try it out from John Lam's blog post here.

Today's IronRuby drop is still a very early version, and several language features and most libraries aren't implemented yet (that is why we are calling it a "pre-alpha" release).  It does, though, have much of the core language support implemented, and can also now use standard .NET types and APIs.

IronRuby has been architected to take advantage of a new DLR feature we call "Dynamic Sites" - which delivers a fast adaptive call-site method caching implementation.  It also uses the lightweight-code generation features of the CLR.  Lightweight code generation enables dynamic language implementations to create in-memory IL that is then JIT'd into native code at runtime (without ever having to save anything to disk).  This can yield much better runtime performance than interpreted code, and the lightweight codegen feature ensures that once we are finished with the JIT'd code we can optionally garbage collect it to avoid leaking. 

We are releasing today's drop mainly for developers interested in language implementations to start looking at the IronRuby source code, and learn how it was implemented.  Developers interested in playing with an early version of Ruby for .NET can also download it and give it a spin.

IronRuby Project Plans

Next month we will be moving the IronRuby source code repository to be hosted on RubyForge.  As part of this move we are also opening up the project to enable non-Microsoft developers to enlist in the project and contribute source code. We'll then work to implement the remaining features and fix compatibility issues found as more libraries and source are ported to run on top of it. 

The end result will be a compatible, fast, and flexible Ruby implementation on top of .NET that anyone can use for free.

IronRuby "Hello World" Console Sample

If you download and build the IronRuby source code, you are probably wondering "how do I start using it?" 

The easiest way to get started is to run the "rbx.exe" interactive console application that by default is built under the \bin\release directory:

Figure 1

This console shell provides you with the ability to write Ruby code interactively.  After each line, the shell will interactively execute it. 

For example, we could output hello world by typing puts "Hello World":

Figure 2

To output this 10 times in a row, we could type the following:

Figure 3

To use Windows Forms functionality in IronRuby, we could type a require statement that references the System.Windows.Forms assembly, and then use the MessageBox.Show method to display a message in a modal dialog:

Figure 4

IronRuby "Hello World" WPF Sample

One of the benefits of implementing a language on top of .NET is that it enables developers using that language to get full access to the rich framework libraries provided with the .NET Framework.  

For a simple example of this in action, I could create a "HelloWPF.rb" text file and type in the following Ruby code below:

Figure 5

The above code uses the WPF UI framework to create a Window that hosts a StackPanel layout manager that initially contains just a button.  When the button is pressed, a new label control is created and added into the StackPanel (causing it to automatically be flowed in the Window). 

I can then run the above application using IronRuby by passing the "HelloWPF.rb" text file as an argument to rbx.exe:

Figure 6

When I run it I'll get a window with a WPF button (note above I added a nice DropShadowBitmapEffect to it in the code above):

Figure 7

And each time I press the button a new label will be added to the Window:

Figure 8

Not only does having the ability to use all of the .NET APIs provide a lot of power, but you'll notice in the code we wrote how it is possible to naturally integrate .NET APIs into other language syntaxes:

Figure 9

In the code snippet above I'm using the Ruby block language feature (similar to a Lambda expression with C# 3.0 and VB9) to implement a "Click" event handler on the WPF button.  Notice how within the block the standard Ruby naming patterns can be used when accessing any .NET API.  For example, instead of using the "FontSize" property on the WPF Label we are accessing it using "font_size" as the property accessor name. IronRuby automatically handles the naming conversion - enabling developers to program with a consistent naming pattern regardless of their language of choice.


If you are interested in trying out this first early drop of IronRuby, you can download the source and build it here.

You can then download my WPF sample above and run it yourself here (note: you must have .NET 3.0 or 3.5 installed - since those deliver the WPF APIs).  To learn more about WPF, I also highly recommend Adam Nathan's excellent WPF Unleashed book (read the review comments on Amazon to see why).

Hope this helps,



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021  |  Page Processed at 2021-12-05 11:21:47 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search