InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

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

Posted by Werner Schuster on Sep 12, 2008

Sections
Development
Topics
Dynamic Languages ,
Rich Client / Desktop ,
Runtimes ,
Ruby
Tags
Apple ,
GUI ,
Visual Studio ,
Ruby1.9 ,
ParseTree
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.

No comments

Watch Thread Reply

Educational Content

Jesper Boeg on Priming Kanban

In this interview, Jesper Boeg, author of the new InfoQ book – Priming Kanban, discusses the keys to using Kanban effectively, and how to get started if you are currently using other approaches.

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.