Sunday, December 17, 2006

Creating GUI with IronPython, Groovy and JRuby

I was curious what are the differences between the following tools when it comes to creating a GUI desktop application:
  • IronPython (with Windows Forms)
  • Groovy (with Swing)
  • JRuby (with Swing)

I prepared a simple example for each of them. The application shows a form with one button. This button when clicked changes its own text.


tested with 1.1a1 (1.1) on .NET 2.0.50727.42

  • Native windows
  • Possible to pass parameters in the constructor (form = Form(Text="test"))
  • Nice += for event handlers
  • As such a simple example it should work with Mono on Linux, however not sure about more complicated ones.

import clr
from System.Windows.Forms
import Application, Button, Form
from System.Drawing import Size

form = Form(
Size=Size(200, 200)

button = Button(Text="OK!")

def onClick(event, handler):
button.Text = "Pressed!"

button.Click += onClick


tested with 1.0-RC-01
(Thanks to Marcin Domański for his help on this example)
  • Cross-platform
  • Elegant event handlers api (actionPerformed:click)
  • Possible to pass parameters in the constructor (as in IronPython)

import javax.swing.JFrame
import javax.swing.JButton

def frame = new JFrame(
size:[200, 200],

def click = {
event -> event.source.text = "Pressed!"

def button = new JButton(


tested with 0.9.2
  • Cross-platform
  • JRuby allows you to call Java code using the more Ruby-like underscore_method_naming and to refer to JavaBean properties as attributes.
  • Event handlers using classes is not nice

require 'java'
include_class "javax.swing.JFrame"
include_class "javax.swing.JButton"
include_class "java.awt.event.ActionListener"

frame ="Hello")
frame.defaultCloseOperation = JFrame::EXIT_ON_CLOSE

button ="OK!")

class ClickListener < ActionListener
def actionPerformed(event)
event.source.text = "Pressed!"



Update: There is a simpler Groovy version with SwingBuilder.

Tuesday, November 21, 2006

Ruby For Rails review

Ruby for Rails is a book written by David A. Black, a well known guru in the Ruby community

In short: The book is great.

I'd recommend it for all people who have already tried Rails and appreciate its magic, but now want to understand what's going on under the hood.

What is Ruby On Rails?

In case you have lived on another planet for the last year, Ruby on Rails is a web development framework. For those of you who know how Django works you can read a good comparison of Django and RoR.

The structure of the book is organised as follows:

Chapter 1 The Ruby/Rails landscape
Chapter 2 Ruby building blocks.
Chapter 3 Built-in classes and modules
Chapter 4 Rails through Ruby, Ruby through Rails

Quoting the author the goal of this book is:

(..) That's the goal - to be able to bring Ruby skills to bear seamlessly on Ruby tasks. Whether it's writing an action, or a method in a helper file, or a highly specialized suite of methods like the rankings and favorites facility in the music store application, the ideal situation is one in which you have a large number of programming techniques at your command and you use whichever ones help you get your application to do what you want it to do.

Having finished this book I feel that not only my Ruby skills got enhanced but also I understand more about Rails code. It's much easier now to follow the discussions about creating DSLs using Ruby and also actually creating my own DSLs. Even though not all of the Rails features are covered in the book, (like migrations or plugins) I'm able to see how simply they are designed. It wasn't easy, though (almost 500 pages). Finishing this book took me some time. I tried to follow most of the examples in the book but it was worth the time spent on it.

I really enjoyed the chapters about dynamic programming. They cover the whole family of eval methods, the singleton class. All of these are heavily used when you want to create a DSL.
Another very useful chapter for me was the one about regular expressions. I'm this type of developer who keeps forgetting details how to prepare a more complex regex.

Thanks to the Ruby for Rails book I feel the Ruby way better. I think it's very important to understand the whole philosophy of a language. I remember (back in December 2004) my first Ruby on Rails code was either looking exactly as in tutorials or when I needed to build something more complicated it was actually Java coding using Ruby ;-) I was in a similar situation when I started working with Python. It was very tempting to just code Ruby using Python. Thanks to Michael's patience I think I'm getting better with Python now :-)

Let's take the following Rails code as an example:

class Edition < ActiveRecord::Base
belongs_to :publisher

All Rails newbies can tell you what this code does. I'm not really so sure if all of them are able to explain how is it actually done under the hood.

I like to call Rails a DSL for creating web applications. Despite the whole simplicity it provides it also means it is an additional layer of abstraction.

Quoting Joel Spolsky :

All non-trivial abstractions, to some degree, are leaky.

We must understand how is the abstraction created so that when in a need we can easily fix any problem that appears during Rails development.

Ruby for Rails is an excellent book. It covers all important topics about Rails development and highlights the Ruby way. If you already created some Rails applications but still feel there is some magic around you definitely should read this book.