IronRuby brings the two together

InformationWeek Staff, Contributor

April 29, 2010

5 Min Read

IronRuby 1.0, a recently released implementation of Ruby hosted in the .NET

Dynamic languages such as Ruby offer something that static languages don't: simplicity. They're designed to get jobs done fast and easy. And since most of the action occurs at runtime, developers can focus on writing better code.

Microsoft added support for dynamic languages four years ago in the .NET Framework by implementing the Dynamic Language Runtime (DLR) component on top of the .NET Common Language Runtime, which provides special services for dynamic languages. IronRuby was one of the first languages identified as a candidate for .NET dynamic language support.

With .NET, everything is built on top of the CLR, so sharing code among languages is easy. IronRuby 1.0 has been released with full source code under the Microsoft Public License. (Download IronRuby at ironruby.codeplex.com.)

IronRuby's performance is excellent. It's two to four times faster than the main Ruby language implementation, according recent benchmarks, making it a top choice for a Ruby interpreter on Windows.

In one recent project, I needed to detach a folder from Subversion supervision, and my only option was to export the files to a different folder. I had to do this quickly, so I turned to Ruby. While I hadn't intended to use IronRuby, I figured out that Subversion SVN files were read-only, and I needed to pull over all of them using recursion to make them available for deletion. A Web search revealed that Windows Management Instrumentation provided an easy solution: I used IronRuby and .NET's System.Management assembly to execute the WMI commands from my Ruby code. It took me all of 10 minutes to develop the tool.

IronRuby is a bridge between Ruby and .NET, making the entire .NET Framework available to Ruby developers, including frameworks such as WPF, Silverlight, and ASP.NET. For .NET developers, the whole Ruby world is accessible, including Ruby on Rails, testing frameworks such as Cucumber, and Ruby's powerful built-in capabilities.

The DLR makes the task of calling code straightforward, making IronRuby great for adding extensibility features to existing .NET apps. To execute IronRuby code, all you write is:


ScriptEngine engine = IronRuby.Ruby .CreateEngine();
engine.Execute("puts 'Hello from IronRuby'");

You can also set variables to be used within scripts and get return values from executed code. The simplicity of this process will ensure that this kind of extensibility is common in the near future.

Internal Tools And POCs

C# code's execution process is similar to IronRuby's, but it skips the DLR and goes directly through the CLR to get executed on the target machine. This similarity, along with the ability to easily use .NET assemblies in IronRuby code, makes IronRuby ideal for .NET developers to use to write internal tools and proofs-of-concepts; they can use Ruby, but stay within the .NET framework boundaries. The code can be written faster, while letting developers experience another language.

When it comes to testing, IronRuby brings with it innovative Ruby testing frameworks that can change the way .NET developers test code by making the process simpler, faster, and more interesting than before.

For example, the RSpec testing framework provides a domain-specific language for unit tests. The following RSpec code tests a .NET method:


describe System::Collections::Stack, "Count" do
        it "returns 0 for empty stack" do
                stack = System::Collections::Stack.new
                stack.Count.should == 0
        end
end

Silverlight

Silverlight provides a rich GUI for Web applications, much like Adobe Flash. Until Version 2, developing Silverlight apps could be done solely by .NET static languages like C#. Since Silverlight 2 includes the DLR, it allows writing Silverlight apps in dynamic languages like IronRuby.

The Silverlight and DLR bundle brings great things to both Ruby and .NET programmers. Ruby developers get a rich Internet application framework they can use in their language, and .NET developers can use the dynamic capabilities of Ruby, such as the ability to create a REPL (Read-Evaluate-Print-Loop) window.

Microsoft's Gestalt project took Silverlight in a different direction, letting developers interact with the browser in Silverlight-supported languages, rather than in JavaScript. Gestalt allows adding <script> tags that contain IronRuby code (IronPython and XAML are supported as well), replacing JavaScript with Ruby for client-side scripting.

To use it, include a JavaScript file named dlr.js, then write Ruby code inside <script language="ruby"> tags. For example, the following HTML page contains a textbox and a button: Once the user writes his or her name and clicks "Congrat!", a Ruby script shows an alert with a welcome message and adds the message to a <div> element on the page:


<html>
<head>
        <script src="dlr.js" type="text/javascript"></script>
</head>
<body>
  <input type="text" id="full_name"/>
  <input id="say_hello" type="button" value="Congrat!" /><br />
  <div id="txt" style="font-size:100px; color:green"></div>
  <script language="ruby">
    document.say_hello.click {
      msg = "Hello #{document.full_name.value}"
      window.alert msg
      document.txt.innerHTML = msg
    }
  </script>
</body>
</html>

Ruby On Rails

Ruby on Rails is an MVC-driven framework widely used to develop Web applications for big sites like Twitter, Yellow Pages, Hulu, and others. IronRuby lets .NET developers use their C#/VB.NET codebase and still take advantage of the Ruby on Rails framework. Furthermore, since the .NET framework is integrated with Microsoft's Internet Information Services, IronRuby becomes the top choice for running Ruby on Rails apps on IIS.

In sum, making Ruby part of the .NET Framework via IronRuby, offers enormous benefits to .NET developers, just as bridging .NET and IronRuby benefits Ruby developers. As soon as both developer camps understand the potential in this, we'll see IronRuby projects erupting everywhere. It's as simple as that.

Shay Friedman is a Microsoft Visual C# amd IronRuby MVP, and author of IronRuby Unleashed.

You can write to us at [email protected].

Never Miss a Beat: Get a snapshot of the issues affecting the IT industry straight to your inbox.

You May Also Like


More Insights