Apply IronRuby to Get Started Building Next-Generation UIs

etting popular open source software (OSS) languages running on Microsoft .NET started when Microsoft adapted Python, creating the .NET version called IronPython. The latest OSS project to get the Microsoft treatment is IronRuby, which is part of a new Microsoft focus on dynamic languages. IronRuby and IronPython are big players in Microsoft’s Silverlight project, which is based on the Dynamic Language Runtime (DLR).

Ruby is a relatively new language with a large and enthusiastic following in the Web 2.0 world, primarily generated by Ruby on Rails (RoR). Created by David Heinemeier Hansson of 37signals, Ruby on Rails is a web framework intended for developing database-driven applications using the Model-View-Controller (MVC) pattern.

The Language
Ruby was first released to the general public in 1995. Heavily influenced by Perl and Smalltalk, as a dynamic and heavily object-oriented language, it also shares some similarities to Python. Functionally, the language uses a single pass interpreter, although both the JRuby and IronRuby implementations make use of a virtual machine approach to improve performance.

However, Ruby has some basic philosophical differences from its progenitor languages. Unlike Python, strings are mutable, meaning they can be changed in place?and you can’t copy one by simply making a copy of the reference. Depending on how you implement heavy string manipulation tasks, such mutability could have performance implications. The languages use different object models, and while zealots on both sides will argue the merits of each, it really comes down to personal preference in the end. Programmers tend to stick with languages they’re comfortable with, because comfort leads to higher productivity.

IronRuby: The Project
John Lam is the creator and chief enthusiast behind RubyCLR, an early implementation of the Ruby language on top of the .NET framework. The whole idea for RubyCLR came from John’s interest in Avalon (now Windows Presentation Foundation), Indigo (now Windows Communication Foundation), and the Ruby language. He used a project for his two-year-old son as the motivation to build the tool that eventually became RubyCLR.

In October of 2006 John Lam announced he was going to work for Microsoft and would continue his efforts there. IronRuby is the result of those efforts. The name pays homage to IronPython and the groundwork laid by Jim Hugunin and others. John Lam’s blog has a number of entries about IronRuby including installation instructions and a link to download the first alpha sources.

Building IronRuby

?
Figure 1. Successful Build: You can see the output that results from building IronRuby targeting the .NET 2.0 framework.

You can download the IronRuby source from John Lam’s blog and build the Ruby interpreter from scratch. To do that, you’ll need to install the Microsoft .NET 2.0 redistributable (or later) before you’ll be able to build the code. You need to make only one small change in the build.cmd file to point at different versions of the .NET framework, which lets you target either the 2.0 or the latest 3.5 framework release. Figure 1 shows the result of building against the .NET 2.0 framework.

Downloading IronRuby
If you’re not interested in building IronRuby directly from the source, you can download a utility called DLR Pad. This tool includes DLL files for both IronPython and IronRuby. DLR Pad was created as a tool to facilitate interactive programming with XAML and either IronPython or IronRuby. The key to the tool is the use of the DLR.

?
Figure 2: XAML Application: Here’s DLR Pad loaded with a simple XAML-based UI and some Ruby code.

The download contains the DLRPad.exe file along with additional DLL files for IronPython, IronRuby and other supporting libraries. You must have the .NET 3.5 runtime installed for this application to work, but it works on both Windows XP SP2 and Windows Vista.

Figure 2 shows a screen shot of DLR Pad loaded with a simple three-button-pane XAML example.

The code for this application uses a nice feature of Ruby that lets you define constants in a separate file and then “require” that file in the same way you would use include to include a file in C. In Figure 2, you can see the require command in the code, which causes Ruby to include a file named wpf.rb that defines some shortcuts, as follows:

   # Reference the WPF assemblies   require 'PresentationFramework, Version=3.0.0.0,       Culture=neutral, PublicKeyToken=31bf3856ad364e35'   require 'PresentationCore, Version=3.0.0.0,       Culture=neutral, PublicKeyToken=31bf3856ad364e35'      # Initialization Constants   Window = System::Windows::Window   Application = System::Windows::Application   Button = System::Windows::Controls::Button   StackPanel = System::Windows::Controls::StackPanel   Label = System::Windows::Controls::Label   Thickness = System::Windows::Thickness   DropShadowBitmapEffect =       System::Windows::Media::Effects::DropShadowBitmapEffect   Vertical = System::Windows::VerticalAlignment

Code Examples
IronRuby makes it relatively easy to work with Windows Forms. This short program creates a simple form with two buttons. Clicking the first button creates a second modal dialog that the user must close before continuing. Here’s the code:

   # Load the .NET windowing assemblies   require 'System.Windows.Forms'   require 'System.Drawing'      # Define some aliases   Forms = System::Windows::Forms   Point = System::Drawing::Point   Size = System::Drawing::Size      # Create a windows form instance   frm = Forms::Form.new   frm.Text = 'IronRuby Demo'   frm.Size = Size.new 200,75      # Create a popup button   popup = Forms::Button.new   popup.Text = 'Clickee Mee'   popup.location = Point.new 10,10   popup.click { |sender, args|       Forms::MessageBox.show 'You click-ed me!' }   frm.Controls.Add popup      # Create an exit button   exitBtn = Forms::Button.new   exitBtn.Text = 'Close'   # The "MouseUp" is a hack to work around a pre-alpha bug    # that makes buttons use the first handler created   exitBtn.MouseUp { |sender, args| frm.close }   exitBtn.location = Point.new 100,10   frm.Controls.Add exitBtn      # And run our application   frm.showDialog

Using a version of IronRuby built with the 3.5 version of the .NET framework gives you access to the WPF code base, which This opens up a number of new possibilities for building user interface (UI) code, such as the interesting challenge of building applications that you can target to either the web or the traditional desktop.

This next code example shows how you can create applications that use Microsoft’s Windows Presentation Foundation (WPF) for the UI and use Ruby to drive the logic. Unfortunately, in the current version you can’t load an external XAML file for the UI.

   # load wpf   require 'wpf'      # Create window   window = System::Windows::Window.new   window.title = 'WPF demo in IronRuby'   window.height = 100   panel = System::Windows::Controls::StackPanel.new   panel.margin = System::Windows::Thickness.new 10   panel.height = 100   window.content = panel      # create our button   btn = Ststem::Windows::Controls::Button.new   btn.content = 'Click Me!'   btn.font_size = 24   panel.children.add btn      # add a nice looking drop shadow   btn.bitmap_effect =       System::Windows::Media::Effects::DropShadowBitmapEffect.new      # the number of times the button has been pressed   clicked = 0      btn.click do |sender, args|      # cycle between Hello and World until the 5th click and then..         if clicked > 3         btn.content = 'Annoyed Yet?'      else         if btn.content == 'Hello'            btn.content = 'World'         else            btn.content = 'Hello'         end         clicked += 1      end   end      # run the application   app = System::Windows::Application.new   app.run window

Bottom Line
While IronRuby shows promise, it’s still definitely a work in progress. IronRuby lacks the same level of support as JRuby, which supports Ruby on Rails and makes it possible to build a RoR application that will run on a J2EE server. IronRuby has no direct RoR support at this time.

While you’re probably not going to build a complex program with the current tools, you can get started with the Ruby language and .NET. Tools like DLR Pad can help you test the interaction between the Ruby language and XAML for building next-generation user interfaces. You can get more information from the IronRuby discussion forum.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may