BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News MacRuby 0.3 Release Brings Interface Builder Support, HotCocoa for GUI Building

MacRuby 0.3 Release Brings Interface Builder Support, HotCocoa for GUI Building

This item in japanese

MacRuby 0.3 is now available:
[A] big change [is] the method dispatcher, which is now entirely based on the Objective-C runtime. MacRuby is now using the Objective-C runtime to implement the Ruby class semantics and also to dispatch pure Ruby methods. This is an important change because it simplifies a lot the core implementation and also eliminates ambiguities between both worlds.
 [..]
On the pure Ruby side of things, a lot of bugs have been fixed and we are now able to run some commands of RubyGems. Installing simple gems should work. Don't expect MacRuby to run Rails yet, though!
MacRuby now supports the creation of GUIs with Cocoa - actually there are two ways of building GUIs. One way is to use the Interface Builder (IB) that comes with Apple's XCode. The GUIs created with the IB can be connected to Ruby classes via actions and outlets (which hold references to GUI components). MacRuby ships a tool - written in Ruby - to create the metadata necessary to map from Ruby code constructs, like accessors or methods, to actions or outlets.

The rb_nibtool is written in Ruby and uses Ruby 1.9's Ripper library. Ripper takes a Ruby source and makes it accessible to Ruby code - either as a stream of Lexer tokens an s-expr (similar to ParseTree which isn't available on Ruby 1.9). The rb_nibtool takes Ruby source files, figures out the class name for the .nib file; attr_accessor, attr_writer and a few other calls are interpreted as outlet definitions, ib_action followed by an identifier defines an action. Finally, all this gathered data is turned into a .nib file which connects the GUI definitions to Ruby code.

As an aside: creating GUIs with MacRuby and the Interface Builder is one option for Ruby on OS X - on Windows, the Ruby In Steel IDE offers GUI building with Visual Studio either with IronRuby or the MRI using the Ruby Connector.

Another way to build GUIs is HotCocoa, shipped with MacRuby, which allows to create GUIs using a builder-style notation, similar to other Ruby tools like Ruby Shoes or other Ruby GUI libraries. A quick look at HotCocoa's implementation shows how build method names are mapped to Cocoa GUI controls. At the moment there is little to no documentation about HotCocoa, so the HotCocoa source and the examples shipped with MacRuby are really the only way to find out how to use it.

HotCocoa, like other Ruby toolkits, comes with a tool that sets up a skeleton application:
hotcocoa classlist 
creates a new application with all necessary libraries and settings in place.
To give an idea of what a HotCocoa program looks like, here a short demo that lists all loaded classes and their ancestors in a table.  This code builds the GUI - to try this, copy the snippet into the lib/application.rb file of a generated HotCocoa skeleton application):
def start
 application :name => "Classlist" do |app|
 app.delegate = self
 window :frame => [100, 100, 500, 500], :title => "Classlist" do |win|
# Add a button to - clicking shows the data in the table
win << button(:title => "Show classes", :bezel => :regular_square).on_action {
klasses = []
ObjectSpace::each_object(Class){|x|
 klasses << {:klass => x.to_s, :ancestors => x.ancestors.join(',')}
}
@table.data = klasses
 }
 # create the table
@table = table_view(
 :columns => [
column(:id => :klass, :text => "Class"),
 column(:id => :ancestors, :text => "Ancestors")
] )
# put the table inside a scroll view
win << scroll_view(:layout => {:expand => [:width, :height]}) do |scroll|
scroll << @table
end
win.will_close { exit }
end
end
end

The application layout comes with a Rake file which handles all the details for running - to launch the application, run:
macrake  

More information about MacRuby is available in InfoQ's interview with Laurent Sansonetti.

Rate this Article

Adoption
Style

BT