Build A Mac Application From Scratch Using MacRuby and Hotcocoa

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

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 MacRuby-0.7.1   cd MacRuby-0.7.1   sudo rake install

Install using the standalone binary installer(dmg):

“MacRuby Installer”:

After install you can check version with the command:

:~/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

Creating a Mac Application

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

    1. Create or Init the project:
      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.


    1. Run the application we just created:
      macrake run


      macruby lib/application.rb

      You should see a window with some text.


    1. Add buttons to the window:Open lib/application.rb in your editor and replace the content with: (
      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("")  		goog_price = result.strip.split(',')[2]  		alert :message => "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.

    2. 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:


  1. 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

    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:

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.