<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>8th Light Blog: RubyCocoa Tutorial</title>
    <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In the minds of the craftsmen...</description>
    <item>
      <title>RubyCocoa Tutorial</title>
      <description>&lt;p&gt;Admit it, you&amp;#8217;re new to Rails.  You just got your new spiffy Rails job, and they gave you a Mac, formerly the computer of long-haired starving-artist types.  It&amp;#8217;s got pretty apps with and even a Remote Control, and it&amp;#8217;s got you thinking about making desktop applications.  Then you remember the Resource Files, C++, Windoze, MFC.  Ugh!  Never mind.  &lt;/p&gt;

&lt;p&gt;If that sounds excactly right, well then you&amp;#8217;re me and I want my body back, but if it sounds close to right then I have great news for you.  RubyCocoa!  Started by Fujimoto Hisa and continued on at http://rubycocoa.sourceforge.net, RubyCocoa allows you to make complete OS X applications in Ruby, using all the features of the Cocoa framework.  There&amp;#8217;s already a few applications out there in it, and a lot of great information, but what I found is that if you&amp;#8217;re a complete newbie to Cocoa/OS X/Interface Builder and Objective-C like I was there&amp;#8217;s no good step-by-step tutorial.  Until now! (Tada!)  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What the heck is Cocoa&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cocoa is an application framework for writing applications on OS X.  That really doesn&amp;#8217;t tell me much does it?  How about a diagram:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-08_cocoadiagram.jpg" alt="Cocoadiagram"/&gt;[1]&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s an application layer on the Mac OS, with all the pretty buttons, scrollbars, and other such widgets that you expect from OS X applications.  These are used through the Objective-C classes included in the framework of your application.  Wait, I know what you&amp;#8217;re thinking, &amp;#8220;C?  Did you say C!  Are you out of your mind?  I&amp;#8217;ve never been so lied to in my life!&amp;#8221;  Relax my friend and keep reading.  I promise you there is no C in this tutorial, well except&amp;#8230;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Objective-C&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Objective-C is the preferred development language for applications on the OS X platform.  It was originally developed by Brad Cox as an object-oriented extension to C, and is compiled in the gcc compiler.  It&amp;#8217;s primarily influenced by SmallTalk, much like Ruby.  If you know C or C++, and really if you are over 25 and a programmer you probably do, then you should be able to learn Objective-C quickly.  Fortunately you don&amp;#8217;t, but you will need to understand some bits and pieces of the syntax in order to translate Cocoa documentation to Ruby.  I&amp;#8217;ll point out where this was done in the tutorial.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting RubyCocoa&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You wanna write Cocoa apps in Ruby, you need RubyCocoa.  All those Objective-C classes are available through Ruby calls, removing the need for such Objective-C necessities such as memory allocation and deallocation, keeping your code simple and beautiful.  Guess you&amp;#8217;re going to have to install it.  Here&amp;#8217;s the subversion call:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;svn co https://rubycocoa.svn.sourceforge.net/svnroot/rubycocoa/trunk/src rubycocoa&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next we need to build and install ruby cocoa.  You&amp;#8217;ll need XCode installed for this to work, and for the rest of the tutorial to work.  I&amp;#8217;m assuming that you&amp;#8217;ve been developing in Ruby for more than a few hours, so you&amp;#8217;ve installed some gems.  If so you probably already had to install XCode.  If not then you&amp;#8217;ll need to insert the OS X setup disc that came with your Mac and install it.  I&amp;#8217;ll wait.  Sheesh that took a while.  Next, from the directory where you checked out the source code, do the following:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ ruby install.rb config
$ ruby install.rb setup
$ sudo ruby install.rb install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The preliminaries are over, let&amp;#8217;s code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your first project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is where the fun begins.  We&amp;#8217;re going to make an app together, and as we go I&amp;#8217;ll explain the bits and pieces in baby steps.  Afterwards hopefully you&amp;#8217;ll know the basics of Cocoa as well as the basics of RubyCocoa.  I&amp;#8217;m borrowing liberally from the basic tutorial done in Objective-C on Apple&amp;#8217;s developer website[2], only done entirely in Ruby.  If you&amp;#8217;ve got 20 minutes, you can do this tutorial.  It takes a half-hour in Objective-C.&lt;/p&gt;

&lt;p&gt;Sadly to start we need to use XCode.  Maybe it&amp;#8217;s me, and my bias against overcharged IDE&amp;#8217;s, but I don&amp;#8217;t like XCode.  It&amp;#8217;s awful busy, and it&amp;#8217;s code formatting in Ruby is &amp;#8230;well my mommy told me if you can&amp;#8217;t say something nice don&amp;#8217;t say anything at all.  So start up XCode and choose File-&gt; New Project.  If you installed your RubyCocoa bundle properly you should be able to select Ruby-Cocoa application from the menu.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-08_xcode_screenshot.jpg" alt="Xcode Screenshot"/&gt;&lt;/p&gt;

&lt;p&gt;Create the Ruby-Cocoa application and name it Currency Converter.  You can put it wherever you want.  Once you&amp;#8217;ve created it you should see this:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-08_xcode_screenshot_2.jpg" alt="Xcode Screenshot 2"/&gt;&lt;/p&gt;

&lt;p&gt;This is your XCode project, similar to a Visual Studio project for my fellow Windoze converts.  The files you&amp;#8217;ll be dealing with in this example are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;CurrencyConverterDemo.app.  You may have named your project differently than I did, but this will be the name of the app file after it&amp;#8217;s built.  This app is distributable to anybody using Mac OS X just like any other application you might have built in Objective-C.  You may be used to writing Ruby scripts that require the user to install various gems or a particular version of Ruby, but thanks to the RubyCocoa.framework also included in the default project this app can be distributed with your users unaware that what language you wrote it in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;MainMenu.nib.  You&amp;#8217;ll be making this your friend.  It&amp;#8217;s where you create your interface, your interface objects, and instantiate them as well - assuming they are NSObjects.  You&amp;#8217;ll also setup actions, outlets, and bindings.  I realize none of this makes sense yet, but bear with me.  Double click the Nib file and you&amp;#8217;ll open Interface Builder, where most of the action is.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Interface Builder and .nib files took a little getting used to for me, perhaps because of my time doing Windows development, because it&amp;#8217;s similar to Windows Forms and Resource files while at the same time being completely different.  When a Cocoa application is loaded the first thing it does is load the Nib file, and instantiate all the objects and variables you&amp;#8217;ve setup.  That&amp;#8217;s right, instantiated objects in a GUI.  There&amp;#8217;s a lot more to this than just setting up how the application looks, you&amp;#8217;re setting up objects and how they relate to each other too.  If you&amp;#8217;ve double clicked the Nib file then you should see four windows.  The first we&amp;#8217;ll be dealing with is the Main Menu.  &lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_menu.jpg" alt="Menu"/&gt;&lt;/p&gt;

&lt;p&gt;Okay it&amp;#8217;s not the best screenshot in the world.  You&amp;#8217;re not reading [coding horror] (http://www.codinghorror.com/), you&amp;#8217;ll have to deal with it.  The first thing to do is change the name to Currency Converter.  Just double-click the name and it will open an editor for you, and you can do that.  Single click the Menu title and you&amp;#8217;ll see the drop-down menu.  Edit that so that each name is also Currency Converter.  Next we&amp;#8217;ll setup the window.  That&amp;#8217;s this empty slate:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_window.jpg" alt="Window"/&gt;&lt;/p&gt;

&lt;p&gt;First things first, you don&amp;#8217;t want to let our little app be called Window do you?  You can&amp;#8217;t double-click here, but you can use the Inspector.  The Inspector shows you all the properties for whatever object you have selected in the application.  This comes in real handy when you don&amp;#8217;t know a keyboard shortcut.  Hit Command-Shift-I to bring up the Inspector.  That gives you this screen:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_inspector.jpg" alt="Inspector"/&gt;&lt;/p&gt;

&lt;p&gt;See that field that says Window Title - change it to Currency Converter as well.  Let&amp;#8217;s change the size too.  In the Inspector click the drop-down window that says Attributes and select Size.  In the Content Rectangle section there is a drop down box that says Width/Height and next to it two windows with Width and Height.  Set the size to 400x200.  That&amp;#8217;s a pretty good starting point.  We&amp;#8217;ve got a blank window, think we could use some controls?  See that big window on the right - the one that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_cocawidgets.jpg" alt="Cocawidgets"/&gt;&lt;/p&gt;

&lt;p&gt;Yipee Toys!  The screenshot above shows the Controls view, which isn&amp;#8217;t actually what we want because we won&amp;#8217;t be adding any buttons yet.  We&amp;#8217;re adding some text views.  Along the top of the window are choices for the different types of Cocoa objects we can add to our application.  Select this one:  &lt;img src="http://blog.8thlight.com/files/2007-08-11_textwidgets.jpg" alt="Textwidgets"/&gt; so we can start adding text objects to our window.  First we will add three edit boxes.  Simply take one of the empty edit boxes and drag it to your window.  When you drag your Window over you&amp;#8217;ll see blue lines appear.  If you lock to those you can take a big step in meeting Apples User Interface guidelines.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_text_box.jpg" alt="Text box"/&gt;&lt;/p&gt;

&lt;p&gt;You&amp;#8217;ve added the first, but to make life easier on yourself hit Apple-D to make the other two.  Make sure you align then on the right blue line, and you should also see a blue line on the top of each box, which marks equal spacing between each edit box.  Next add the System Font Text to the left of each box.  Add the first one to align with the top box, then again use Apple-D to duplicate them.  Align them with each of the boxes, using the blue lines.  It should look like this:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_edit_boxes_and_text.jpg" alt="Edit Boxes and Text"/&gt;&lt;/p&gt;

&lt;p&gt;We aren&amp;#8217;t exactly finished with our application yet are we?  For starters we need to change that to useful text.  Double click each text box and change the text to read Amount of Money:, Conversion Rate:, New Amount: respectively.  Um - uh oh.  This doesn&amp;#8217;t look right does it?&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_bad_text.jpg" alt="Bad Text"/&gt;&lt;/p&gt;

&lt;p&gt;We&amp;#8217;re gonna have to fix that.  Holding down the shift button click each box.  This should select them all.  Next choose Layout from the top menu, and choose Size To Fit.  This will make all the boxes size to fit the text, and extend the top box to include the text you entered earlier.  If any of your boxes are two lines long it&amp;#8217;s because you added a return to the text when you edited it the first time.  Go ahead and re-edit the text, deleting the return then size to fit the box again.  Now select each text element individually, and head to the Inspector.  It should already be open from previous uses but if not use Apple-Shift-I to open it up.  Select the attributes for the text object in the drop-down and set the alignment to the right.   &lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_alignright.jpg" alt="Alignright"/&gt;&lt;/p&gt;

&lt;p&gt;Once again select all the text boxes again making sure you start from Amount of Money:.  Choose Layout-&gt; Same Size.  Then choose Layout-&gt;Alignment-&gt;Right edges.  Drag all those boxes to the left side of the window, aligning with the blue line when it appears.  I use the keyboard for this so that I don&amp;#8217;t accidentally move the text up or down.  Finally drag the edit boxes over to the text, again aligning with the blue line.  You do this by selecting all of them, then moving them to the left.  At this point the box should look like this:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_window_with_text.jpg" alt="Window With Text"/&gt;&lt;/p&gt;

&lt;p&gt;Looks like it&amp;#8217;s a little big, but we don&amp;#8217;t want to resize it until we add our button.  In the Cocoa window go back to the controls view. with this button: &lt;img src="http://blog.8thlight.com/files/2007-08-11_control_button.jpg" alt="Control Button"/&gt;.  Not surprisingly we&amp;#8217;re going to add a button to the window, in order to actually convert the currency.  Take the button and drag it over to the Window, putting it underneath the boxes and once again aligning with a blue line that will appear above it.  Double click the button and rename it to Convert.  Now let&amp;#8217;s take a peek at one of the cool parts of Interface Builder.  With the button selected, hold the option key and move your mouse over the box.  You should see this:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_option_bars.jpg" alt="Option Bars"/&gt;&lt;/p&gt;

&lt;p&gt;Now by moving the keyboard left and right we can actually put the button directly in the middle of the three boxes.  Now it&amp;#8217;s time to shrink the screen to fit, just like you would shrink any window until the ever-so-ubiquitous blues appear.  Do that now.  Finally just for a flourish let&amp;#8217;s add a little line between the button and the boxes.  Drag the horizontal line from the controls on to the window right underneath the boxes, aligning with yet another blue line.  Extend it to the blue lines on the left and right to make it full sized. You should have a dialog box that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_final_dialog.jpg" alt="Final Dialog"/&gt;&lt;/p&gt;

&lt;p&gt;I swear we&amp;#8217;re almost to Ruby code, so quit complaining and save your interface.  We now need a way to interact with this pretty little picture.  The way that&amp;#8217;s done in the land of Cocoa is through Actions and Outlets.  Actions are methods that can be triggered by other objects, whereas outlets are pointers to other objects.  Essentially if you want to &lt;em&gt;do_ something you need an action if you want to _set or get&lt;/em&gt; something you need an outlet.  The first thing to setup is an action - the Convert button needs to _do_ something does it not?  In order to connect the action from the button we need to have a class.  That&amp;#8217;s right - we&amp;#8217;re going to make some Ruby code.  There&amp;#8217;s one more window we haven&amp;#8217;t yet used in Interface Builder:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_instances_view.jpg" alt="Instances View"/&gt;&lt;/p&gt;

&lt;p&gt;Click the Classes tab so we can crate two Ruby classes, both of which will inherit from NSObject.  That&amp;#8217;s right, Ruby classes inheriting from Objective-C classes.  Like a Picasso it&amp;#8217;s disturbing and beautiful at the same time.  The Classes view lists all the available Cocoa classes, so scroll to the left side and select NSObject.  Then hit enter and it will automatically create a class named ConverterController.  &lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_class_view.jpg" alt="Class View"/&gt;&lt;/p&gt;

&lt;p&gt;Now this creates a class in the Nib file so that the interface knows about it, but because we&amp;#8217;re using Ruby we don&amp;#8217;t actually generate our class from it.  Let&amp;#8217;s also create a class called Converter.  Even though it&amp;#8217;s overkill we&amp;#8217;re going to create a MVC framework here, simply for demonstration purposes.  After you&amp;#8217;ve done that select ConverterController and use Apple-Shift-I to again bring up the inspector.   You should see this view:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_currency_converter_inspector.jpg" alt="Currency Converter Inspector"/&gt;&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s think for a moment.  What is the controller going to need to do.  When the button is clicked it needs to get the values for the original amount and conversion rate, calculate the new amount, then set the new amount.  This means we will need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Action - for the convert button&lt;/li&gt;
&lt;li&gt;Outlet - for getting the original amount&lt;/li&gt;
&lt;li&gt;Outlet - for getting the conversion rate&lt;/li&gt;
&lt;li&gt;Outlet - for setting the new amount&lt;/li&gt;
&lt;li&gt;Outlet - for sending the data to the model to calculate the new amount.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Admit it, you were going to just do the calculation right then and there in the Controller class.  Say 10 Hail Matz&amp;#8217;s for punishment.  Good job.  Adding these outlets and actions is pretty self explanatory.  Just click add.  There&amp;#8217;s one gotcha.  The action is a method.  Methods in Objective-C will have a colon at the end and you must have the colon at the end in your description of the outlet, even though the method you actually declare will not have one.  Otherwise things won&amp;#8217;t be found.  When adding outlets there is a drop down for the type.  This has a use in Objective-C, but we&amp;#8217;re doing Ruby.  Leave it at id.&lt;/p&gt;

&lt;p&gt;What we&amp;#8217;ve created here is a class definition, so that Objective-C can call the classes you haven&amp;#8217;t implemented yet.  However we also need to instantiate the class, otherwise there&amp;#8217;s nothing to connect.  Everything you&amp;#8217;ve just defined is a variable, it needs to be set to something.  To do that select ConverterController and select from the top-level menu Instantiate ConverterController.  Then do the same for Converter.  This lets the Cocoa Framework know that when it starts up it needs to create each of these objects.  You won&amp;#8217;t be calling new here, in fact you never define initialize or call new on a Ruby Cocoa object.  That&amp;#8217;s a topic for another day however.  In the Instances view you should now see this:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_instances_classes_instantiated.jpg" alt="Instances Classes Instantiated"/&gt;&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m going to re-iterate something.  These aren&amp;#8217;t a resource file, or an XML definition.  What you&amp;#8217;ve done here is create real live objects.  If you attempt to build and run now it will fail because you haven&amp;#8217;t written an underlying implementation, but the objects are there.  They will be allocated.  They&amp;#8217;re the real thing.  Now look at that tiny exclamation point next to ConverterController.  What it says is you have unconnected outlets.  When the Cocoa application starts up it&amp;#8217;s going to attempt to link those outlets and if you haven&amp;#8217;t connected them they will be set to nil.  Let&amp;#8217;s connect them.  To connect a control to an action you hold the Ctrl key and drag from the button to the class.  Think of the connections as following the path of the event, so for example you&amp;#8217;ll drag from the Convert button to the class that will implement the action: ConverterController.  When you do that the Inspector will display automatically with the convert: action highlighted.  On the bottom of that dialog click connect.  It should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_connection.jpg" alt="Connection"/&gt;&lt;/p&gt;

&lt;p&gt;You&amp;#8217;ve connected your first action to its target.  Now when you click Convert the action convert in the ConverterController class will be called.  It&amp;#8217;s time to go the other way.  Ctrl-drag another line from the ConverterController to the first edit box and connect it to the appropriate outlet.  Do the same with the other two edit boxes.  You should see this in the Inspector window:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_outlets_without_converter.jpg" alt="Outlets Without Converter"/&gt;&lt;/p&gt;

&lt;p&gt;One more to go.  You don&amp;#8217;t have to go from an instantiated class to an object in the window, you can go from object to object right in the Instances view.  Let&amp;#8217;s do that now to connect the ConverterController to the Converter object, and you&amp;#8217;ve got your final outlet.  Save your Nib file, and go to the XCode window.  On the top window go ahead and click the Build and Go button.  The application should start up just fine displaying your new shiny dialog.  If you click Convert it does absolutely nothing, say it again.  Yea!   We get to write Ruby code to implement those classes now.  About time isn&amp;#8217;t it?  In the action drop down select New File.  In the New file dialog make sure you select Ruby NSObject class like so:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://blog.8thlight.com/files/2007-08-11_new_file.jpg" alt="New File"/&gt; &lt;/p&gt;

&lt;p&gt;Create converter_controller.rb and converter.rb.  Both these will be opened for you in XCode, but I prefer to open the project at this point in Textmate.  Either way your will begin editing your Ruby code.  Let&amp;#8217;s start with the Converter Controller class.  First things first we&amp;#8217;ll delete the boilerplate comments and the require, which is unnecessary.  Next we better rename that class to CurrencyConverter and converter to Converter.  Don&amp;#8217;t blame me, XCode doesn&amp;#8217;t know much about Ruby and has no idea about naming rules.  Now we need to implement those outlets and actions and it couldn&amp;#8217;t be easier.  First the outlets.  Where you would normally type attr declarations and the like add this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ib_outlets :amount_of_money, :conversion_rate, :converter, :new_amount&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;I know I know, it&amp;#8217;s not DRY.  I&amp;#8217;m sorry but Interface Builder does not support generating this code from the GUI with Ruby, at least not yet.  Next the action.  Create an empty method named convert.  Done!  For some perspective in Objective-C you would have had to define this in the interface, with the proper macro, then implement it in the implementation file.  Sure it&amp;#8217;s a few more lines, but you&amp;#8217;re a Ruby programmer so you know just how much nicer four lines can be, especially when you keep adding them up over the course of an entire project.  Build and run your project again.  It will still do a whole lotta nuthin&amp;#8217;, but the console won&amp;#8217;t show any errors about being unable to find CurrencyConverter or Converter.   If you get errors that classnames must be constant it&amp;#8217;s because you forgot to rename the classnames.  You&amp;#8217;ve got an implementation ready to be written, and you can add the code.   Let&amp;#8217;s go into your convert action and get the  values from the outlets that will have the user type in.  Add this to your class:&lt;/p&gt;

&lt;pre&gt;
def convert
  amount_of_money = @amount_of_money.floatValue
  conversion_rate = @conversion_rate.floatValue
end
&lt;/pre&gt;

&lt;p&gt;I know I&amp;#8217;m an idiot right?  There&amp;#8217;s no initialize method so the outlets are both nil.  Okay you got me, so how come when you build and run this the app doesn&amp;#8217;t crash?  Try it now.  Build your new app and add values to those two boxes, then hit convert.  Did it crash?  It didn&amp;#8217;t?  Then I expect an e-mail apology for those nasty thoughts you had about me.  The reason it didn&amp;#8217;t crash is because of the way Cocoa works.  On the start of your application it instantiated the classes necessary and setup all the outlets with the values you specified in the Nib file.  In fact if you &lt;em&gt;were&lt;/em&gt; to add an initialize this code wouldn&amp;#8217;t work, because RubyCocoa does not even allow initialize in classes that inherit from Cocoa classes.  The reason for this is that Cocoa doesn&amp;#8217;t have initialize, it has alloc and init.  You can instantiate your own class, and you should for testing, but don&amp;#8217;t use initialize.  &lt;/p&gt;

&lt;p&gt;There&amp;#8217;s one other thing funny about the code, and that is the name floatValue.  Unfortunately Objective-C methods use camelcase.  I wish RubyCocoa rubified the names, however on the bright side every time you see that naming convention you should be able to instantaneously identify it.  Cocoa object here!  Our object still doesn&amp;#8217;t convert anything, so let&amp;#8217;s finish off the controller&amp;#8217;s implementation.  Add the calculation and the setting here:&lt;/p&gt;

&lt;pre&gt;
def convert
  amount_of_money = @amount_of_money.floatValue
  conversion_rate = @conversion_rate.floatValue

  new_amount = @converter.convert amount_of_money, conversion_rate

  @new_amount.setFloatValue new_amount
  @amount_of_money.selectText @amount_of_money
end
&lt;/pre&gt;

&lt;p&gt;Reads pretty darn well doesn&amp;#8217;t it?  Get a float value out of the amount of money and conversion rate boxes, use the converter object to then convert that to a new amount, then set the float value for the new amount.  Finally we select the text to the amount of money dialog box, so that we can calculate again.  I realize I&amp;#8217;m explaining method calls very quickly, but I&amp;#8217;m not sure there&amp;#8217;s that much to say.  Compare this to the Objective-C version:&lt;/p&gt;

&lt;pre&gt;
#import "ConverterController.h"

@implementation ConverterController

- (IBAction)convert:(id)sender
{
    float rate, currency, amount;

    currency = [dollarField floatValue];
    rate =     [rateField floatValue];
    amount =   [converter convertCurrency:currency atRate:rate];

    [amountField setFloatValue:amount];
    [rateField selectText:self];
}

@end
&lt;/pre&gt;

&lt;p&gt;Honestly that&amp;#8217;s not even fair to the Ruby version, since I could easily shrink down the calls to get that method into two lines and I didn&amp;#8217;t display the header file for ConverterController.  Finally let&amp;#8217;s write the one-line implementation of the converter model:&lt;/p&gt;

&lt;pre&gt;
def convert(amount, rate)
  return amount * rate
end
&lt;/pre&gt;

&lt;p&gt;We&amp;#8217;re done!  Look at that, 90% of the work was getting used to interface builder.  Build and run your app.  Yipee!  You can convert currency now.  You&amp;#8217;re a God.  Well at least a demi-god, like Heracles.  Here&amp;#8217;s hoping this can be a series of articles, and that you keep coming back for more.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;* Footnotes *&lt;/em&gt;
[1] http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/chapter&lt;em&gt;2&lt;/em&gt;section&lt;em&gt;2.html#//apple&lt;/em&gt;ref/doc/uid/TP40002974-CH3-DontLinkElementID_20
[2] http://developer.apple.com/documentation/Cocoa/Conceptual/ObjCTutorial/&lt;/p&gt;

&lt;p&gt;This article is largely a conglomeration of other sources and could not have been completed without the following:  Aaron Hillegass&amp;#8217;s excellent Cocoa Programming for Mac OS X, RubyCocoa.com, the Apple developer website, and of course http://rubycocoa.sourceforge.net.  I wholeheartedly recommend going through Aaron&amp;#8217;s book, using Ruby as your chosen language.  You&amp;#8217;ll be amazed at how fast you can translate the code.&lt;/p&gt;</description>
      <pubDate>Mon, 13 Aug 2007 10:44:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:24b4d8ec-c9f6-4d6b-8edc-e0ece99efe5f</guid>
      <author>Eric</author>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial</link>
      <category>Coding</category>
      <category>Ruby</category>
      <category>Eric</category>
      <category>cocoa</category>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by K. M. Lawson</title>
      <description>&lt;p&gt;A wonderful and humorous tutorial. It would be wonderful if you could update it for the very different environment of XCode 3.1. As is, it has been difficult for me to figure out how to do it.&lt;/p&gt;</description>
      <pubDate>Sat, 14 Jun 2008 13:42:40 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:5bc4784d-d090-4dfc-8028-efd516d2f4ec</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-788</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by Fran&#231;ois Beausoleil</title>
      <description>&lt;p&gt;Great job!  I had problems because I&amp;#8217;m on Leopard too, but I discovered my around without reading the comments.  Good job, Eric.  Thanks for the tutorial.  Now, onto my very own projects!&lt;/p&gt;</description>
      <pubDate>Tue, 10 Jun 2008 04:32:37 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:2b611272-94ff-4897-ae8b-eabed92ffadb</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-783</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by Mikkel Malmberg</title>
      <description>&lt;p&gt;I fount out what was missing. I renamed the controller ConverterController and everything was good.&lt;/p&gt;

&lt;p&gt;Thanks for the best tutorial on RubyCocoa I&amp;#8217;ve read!&lt;/p&gt;</description>
      <pubDate>Thu, 28 Feb 2008 15:03:21 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e9f27de0-04cb-40ab-a570-2460e556d812</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-734</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by Mikkel Malmberg</title>
      <description>&lt;p&gt;I get to the very end and get this error:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2008-02-27 12:03:30.642 CurrencyConverterDemo[2570:613] Could not connect the action convert: to target of class ConverterController&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;On Leopard I made it (almost) all the way. Please lift me the last step :)&lt;/p&gt;</description>
      <pubDate>Wed, 27 Feb 2008 11:05:08 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e93ee04a-8b47-4250-a23a-9a054057eff2</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-733</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by PentiumSlayer</title>
      <description>&lt;p&gt;I installed RubyCocoa-0.13-10.4 on my Mac Mini PPC. I couldn&amp;#8217;t get RubyCocoa to appear in the list of possible project types in Xcode 2. &lt;/p&gt;

&lt;p&gt;What did I do wrong?&lt;/p&gt;

&lt;p&gt;I could install PyObC with no problems. I don&amp;#8217;t want to upgrade to Leopard on this machine, better get a new Mac with Leopard pre-installed.&lt;/p&gt;</description>
      <pubDate>Wed, 13 Feb 2008 00:34:56 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a7c63f33-c698-4f4e-a8f3-df22820e9461</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-728</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by Sergio Pereira</title>
      <description>&lt;p&gt;@Olaf,
I got it working on Leopard with a few tweaks. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To create the Converter and ConverterController in the NIB window, you can go to the Library window, navigate the Objects tree to Library/Cocoa/Objects &amp;amp; Controllers/Controllers and then drag an Object item to the MainMenu.nib window. Rename the created objects to Converter and ConverterController.&lt;/li&gt;
&lt;li&gt;Create your .rb files using the main XCode window just like dewcribed in the tutorial.&lt;/li&gt;
&lt;li&gt;Go back to the Interface builder and select the ConverterController in the NIB window. In the Inspector window, choose the Identity tab (the &amp;#8216;i&amp;#8217; icon) and change the class identity drop down to ConverterController. Do similar for the Converter object in the NIB window.&lt;/li&gt;
&lt;/ul&gt;</description>
      <pubDate>Mon, 11 Feb 2008 00:39:30 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:29d77e98-b21d-4a1a-b36f-4d4ac90fd759</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-726</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by Olaf Klein</title>
      <description>&lt;p&gt;Hi, &lt;/p&gt;

&lt;p&gt;many thanks for the tutorial.
I followed it on Leopard, the interface is built, but MainMenu.nib has no Button-line with 
[instances] [classes] [images]&amp;#8230;
where i could add the classes needed.&lt;/p&gt;

&lt;p&gt;could anyone give a hint to me newbie?&lt;/p&gt;

&lt;p&gt;TIA&lt;/p&gt;

&lt;p&gt;.olaf&lt;/p&gt;</description>
      <pubDate>Sat, 26 Jan 2008 22:42:56 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f70fc657-2456-4203-a7ce-f52e6f9488b9</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-480</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by Radar</title>
      <description>&lt;p&gt;Would you be willing to redo this tutorial with leopard?!!!&lt;/p&gt;

&lt;p&gt;Thanks,&lt;/p&gt;</description>
      <pubDate>Thu, 24 Jan 2008 04:34:02 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f6645208-debc-4061-9345-0f7a9d302906</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-479</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by redochre</title>
      <description>&lt;p&gt;When you&amp;#8217;re creating the converter_controller.rb and converter.rb ruby files you say: &amp;#8220;Next we better rename that class to CurrencyConverter and converter to Converter.&amp;#8221;  &lt;/p&gt;

&lt;p&gt;That should be ConverterController and Converter, right?  I think that may be the solution to John&amp;#8217;s problem, above.&lt;/p&gt;</description>
      <pubDate>Fri, 30 Nov 2007 13:30:56 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:dd5d613e-1353-4ce8-b69b-36a430bde96a</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-425</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by marius</title>
      <description>&lt;p&gt;I have Leopard now with XCode 3.0 and it is so different from your tutorial-pictures that I cannot help but give up&amp;#8230; :( I have the impression it&amp;#8217;s so new that I cannot find anything tutorial-like on the net for this.&lt;/p&gt;

&lt;p&gt;For two days I have been disassembling the RubyCocoa examples that came with XCode 3, but I just can&amp;#8217;t figure it out. Gosh - maybe I &lt;b&gt;should&lt;/b&gt; buy a book :)&lt;/p&gt;</description>
      <pubDate>Thu, 01 Nov 2007 15:06:38 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:97a7b044-5c70-4faa-b50a-5347d3e0f915</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-352</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by bob</title>
      <description>&lt;p&gt;Hi, I go to get to where you&amp;#8217;re about to add the converter controller to the nib file.  There is no class tab (or any tabs at all) in my interface builder.  Could it be the version of XCode I am using?  Please hjalp&lt;/p&gt;</description>
      <pubDate>Thu, 01 Nov 2007 04:40:47 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:9004792d-4a50-4382-8a95-01c62945c3ca</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-351</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by corrado </title>
      <description>&lt;p&gt;hi there, thanks for your great tutorial, but&amp;#8230; I&amp;#8217;m not able to compile rubycocoa. It complains about some missing quicktime stuff, as follows :&lt;/p&gt;

&lt;p&gt;In file included from :6:
/System/Library/Frameworks/QuickTime.framework/Headers/Movies.h:47:33: CoreAudio/CoreAudio.h: No such file or directory
In file included from :6:
/System/Library/Frameworks/QuickTime.framework/Headers/Movies.h:353: error: parse error before &amp;#8216;*&amp;#8217; token
/System/Library/Frameworks/QuickTime.framework/Headers/Movies.h:10807: error: parse error before &amp;#8220;AudioBufferList&amp;#8221;
        from ./gen&lt;em&gt;bridge&lt;/em&gt;metadata.rb:520:in &lt;code&gt;collect'
        from build.rb:101
        from build.rb:50:in&lt;/code&gt;measure&amp;#8217;
        from build.rb:101
        from build.rb:62
config failed&lt;/p&gt;

&lt;p&gt;any idea ?&lt;/p&gt;

&lt;p&gt;thanks again&lt;/p&gt;</description>
      <pubDate>Fri, 12 Oct 2007 05:04:11 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:fba0dae3-1083-42dd-b809-b1bb50c4f669</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-347</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by Eric</title>
      <description>&lt;p&gt;Hi John.  Make sure you have added ConvertController and Convert to your Nib file.  I&amp;#8217;ve had issues before where they can&amp;#8217;t be found, and if memory serves I had to delete the files (from the project, not the file system) then re-add them&lt;/p&gt;

&lt;p&gt;Sorry for the delayed response.  I didn&amp;#8217;t see you were having any trouble. &lt;/p&gt;

&lt;p&gt;Eloy I saw that about 2 weeks after I did this tutorial :)&lt;/p&gt;</description>
      <pubDate>Mon, 01 Oct 2007 18:00:12 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ed29c1fe-233b-4c04-b062-fe6f13500629</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-346</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by John</title>
      <description>&lt;p&gt;Hi.&lt;/p&gt;

&lt;p&gt;I think I followed these instructions to the letter, but when I &amp;#8216;Build and Go&amp;#8217;, I get these messages:&lt;/p&gt;

&lt;p&gt;Currency Converter[483] Unknown class &amp;#8216;ConverterController&amp;#8217; in nib file, using &amp;#8216;NSObject&amp;#8217; instead.&lt;/p&gt;

&lt;p&gt;Currency Converter[483] Unknown class &amp;#8216;Converter&amp;#8217; in nib file, using &amp;#8216;NSObject&amp;#8217; instead.&lt;/p&gt;

&lt;p&gt;And, of course, it does nothing.&lt;/p&gt;

&lt;p&gt;Any idea why it&amp;#8217;s not finding the files? &lt;/p&gt;

&lt;p&gt;Thanks,&lt;/p&gt;

&lt;p&gt;-John&lt;/p&gt;</description>
      <pubDate>Fri, 28 Sep 2007 14:54:55 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ef965d20-d4de-4d6f-82a7-2e9126d53905</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-345</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by Eloy Duran</title>
      <description>&lt;p&gt;Very nice tutorial Eric!&lt;/p&gt;

&lt;p&gt;Just a little note. After setting up the classes in IB you say that there&amp;#8217;s now way to automate the process of converting the class and it&amp;#8217;s outlets/actions into a ruby source file.&lt;/p&gt;

&lt;p&gt;However, we do have a tool for this, which is called rb_nibtool.rb.
Please refer to the RubyCocoa wiki for info about this: &lt;a href="http://rubycocoa.sourceforge.net/RubyWithInterfaceBuilder" rel="nofollow"&gt;http://rubycocoa.sourceforge.net/RubyWithInterfaceBuilder&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cheers,
Eloy&lt;/p&gt;</description>
      <pubDate>Fri, 21 Sep 2007 08:57:05 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:dac8e4be-ceb6-44d6-b74f-4d5bb4d37ac4</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-344</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by zfaktory</title>
      <description>&lt;p&gt;Hi, very interresting.&lt;/p&gt;

&lt;p&gt;If you don&amp;#8217;t like xcode, have you tried the newcocoa gem:
&lt;a href="http://metaatem.net/2007/05/27/your-first-few-days-on-rubycocoa" rel="nofollow"&gt;tutorial&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It has a structure very similar to rails projets.&lt;/p&gt;

&lt;p&gt;You can even write a little ruby script to open your app in Textmate:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/usr/local/bin/ruby
# newapp
if ARGV != []
   puts %x{ newcocoa #{ARGV[0]} }
   %x{ mate #{ARGV[0]} }
else
   puts 'Usage: newapp [appname]'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I put it in &lt;code&gt;/usr/local/bin&lt;/code&gt; and &lt;code&gt;chmod +x newapp&lt;/code&gt;, it&amp;#8217;s very handy and keep you away from xcode. Double clicking on nib directory in mate project drawer open the file in interface builder.&lt;/p&gt;

&lt;p&gt;Have a look also on &lt;a href="http://rubyobjc.com" rel="nofollow"&gt;RubyObjc&lt;/a&gt;, it&amp;#8217;s the same person that maintain &lt;a href="http://rubycocoa.com" rel="nofollow"&gt;rubycocoa.com&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Tue, 04 Sep 2007 06:10:05 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0c789a39-e988-4b07-bf83-ec7592030884</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-329</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by Vasil</title>
      <description>&lt;p&gt;Thanx for this! Much more practical than using Tk (ewh).&lt;/p&gt;</description>
      <pubDate>Fri, 17 Aug 2007 18:49:04 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0fa4b1de-a995-4c96-b5a6-347f50b7dd0d</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-324</link>
    </item>
    <item>
      <title>"RubyCocoa Tutorial" by Marin</title>
      <description>&lt;p&gt;Hi Eric,
I&amp;#8217;ve been programming for many years and recently I switched to mac. Your tutorial was just what I needed to start with this awkward xcode ide &amp;#8230; 
I&amp;#8217;m starting a blog too &amp;#8230; probably will be lots of ruby staff :) 
Thanks a ton ! 
Marin&lt;/p&gt;</description>
      <pubDate>Thu, 16 Aug 2007 15:01:37 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e23e6614-a412-42c3-91ca-28c7fab36f14</guid>
      <link>http://blog.8thlight.com/articles/2007/08/13/rubycocoa-tutorial#comment-323</link>
    </item>
  </channel>
</rss>
