What is MacRuby?

MacRuby“MacRuby is an implementation of Ruby 1.9 directly on top of Mac OS X core technologies such as the Objective-C runtime and garbage collector, the LLVM compiler infrastructure and the Foundation and ICU frameworks. It is the goal of MacRuby to enable the creation of full-fledged Mac OS X applications which do not sacrifice performance in order to enjoy the benefits of using Ruby.” From MacRuby.org

What is Hotcocoa?

HotCocoa is a thin, idiomatic Ruby layer that sits above Cocoa and other frameworks. HotCocoa used to be included in MacRuby but is now managed as a separate gem. This allows HotCocoa to accept contributions and evolve more quickly.

Macruby + Hotcocoa = Build Mac applications without pain (For Rubyists)

Before we get started with the tutorial we need to install MacRuby and Hotcocoa.

Install Macruby from sources:

svn co http://svn.macosforge.org/repository/ruby/MacRuby/tags/0.7.1 MacRuby-0.7.1 cd MacRuby-0.7.1 sudo rake install 1 svn co http : //svn.macosforge.org/repository/ruby/MacRuby/tags/0.7.1 MacRuby-0.7.1 cd MacRuby-0.7.1 sudo rake install

Install using the standalone binary installer(dmg):

“MacRuby Installer”:http://www.macruby.org/files/MacRuby%200.7.zip

After install you can check version with the command:

:~/macruby -v MacRuby 0.7 (ruby 1.9.2) [universal-darwin10.0, x86_64] 1 : ~ / macruby - v MacRuby 0.7 ( ruby 1.9.2 ) [ universal - darwin10 . 0 , x86_64 ]

You can run ruby commands under macruby with the prefix: mac+(irb/rake/gem)

Install Hotcocoa:

Since hotcocoa is now a gem we can use gem install to install it; but notice that we have to install it with macgem:

macgem install hotcocoa 1 macgem install hotcocoa

Creating a Mac Application

So here we are! follow the steps below to create a mac application:

Create or Init the project:

hotcocoa my_first_app 1 hotcocoa my_first_app

You won’t see any outputs after the command but it will create a directory, ‘my_first_app’. The structure will look like this:



The build.yml file stores information like our app’s name, version number, etc. The file we will touch most of time is: lib/application.rb.

Run the application we just created:

macrake run 1 macrake run

or

macruby lib/application.rb 1 macruby lib / application . rb

You should see a window with some text.

Add buttons to the window:Open lib/application.rb in your editor and replace the content with: (https://gist.github.com/708359)

window :frame => [100, 100, 200, 140], :title => "My First App" do |win| win "Quit", :frame => [0, 0, 100, 40] , :on_action => proc { exit}) win "What's GOOG price?", :frame => [0, 10, 150, 40], :on_action => proc { result = Net::HTTP.get URI.parse("http://download.finance.yahoo.com/d/quotes.csv?s=GOOG&f=snl1c1p2") goog_price = result.strip.split(',')[2] alert :message => "GOOG: #{goog_price}" }) win.will_close { exit } end 1 window : frame =& gt ; [ 100 , 100 , 200 , 140 ] , : title =& gt ; "My First App" do | win | win "Quit" , : frame =& gt ; [ 0 , 0 , 100 , 40 ] , : on_action =& gt ; proc { exit } ) win "What's GOOG price?" , : frame =& gt ; [ 0 , 10 , 150 , 40 ] , : on_action =& gt ; proc { result = Net :: HTTP . get URI . parse ( "http://download.finance.yahoo.com/d/quotes.csv?s=GOOG&f=snl1c1p2" ) goog_price = result . strip . split ( ',' ) [ 2 ] alert : message =& gt ; "GOOG: #{goog_price}" } ) win . will _ close { exit } end

What this code does is add two buttons to the window. It adds one button to send the request to get the stock price of GOOG and display the result with a popup box; it adds a second button to quit the app. Run the application again with ‘macrake run’:You will see something like this: Click the “What’s GOOG Price” will display a popup box like:



Package Application:Let’s say we’ve finished our functions in the app; our next step is to package our application. However, before you distribute your application you may want to replace the default icon; what you need to do is put a .icns file in resources/ and update the build.yml file for the icon attribute:

macrake deploy 1 macrake deploy

This command will generate an app file under the directory which we can send to our friends. Oddly enough, this command will copy the entire MacRuby.framework, resulting in a file that will be larger than a normal cocoa application. But there is a solution to reduce the size by: http://isaac.kearse.co.nz/

Tips: To build a functional application you will definitely use lots of interface widgets like UITextField, View, etc. Hotcocoa builds the mapping around these which you can refer to. Check the hotcocoa code files in hotcocoa/lib/hotcocoa/mappings/ for more information.

Resources: