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


Apply IronRuby to Get Started Building Next-Generation UIs : Page 2

IronRuby represents the marriage of the Ruby language to the .NET Common Language Runtime.

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
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 = 
   # 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?'
         if btn.content == 'Hello'
            btn.content = 'World'
            btn.content = 'Hello'
         clicked += 1
   # 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.

Paul Ferrill, based in Chelsea, Alabama, has been writing about computers and software for almost 20 years. He has programmed in more languages than he cares to count, but now leans toward Visual Basic and C#.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date