Skip to content

A Few Other RubyCocoa Tutorials

A few links to some RubyCocoa tutorials I found helpful in addition to the ones you can find on this weblog:

Bowled over by RubyCocoa

This is a wonderful tutorial that both teaches you some basic GUI programming with RubyCocoa while teaching you a testing approach. I don’t recommend it for complete beginners, however.

Selectable Toolbar Icons in RubyCocoa

Create nice selectable toolbar items for your prefs window. Two part tutorial.

RubyCocoa SpeakLine example

Shows you how to create an application that speaks a line you enter. Lots of comments in the code makes this one nice. Uses an example from Hillegass book.

Your First Few Days on RubyCocoa

A lot of people have mentioned this around the net but this is over a year old and I had trouble getting things to work in my newer XCode environment.

RubyCocoa MultipleNibTabView sample

Shows you how to use views housed in separate nibs. Just provides you with some code without much explanation.

Satoshi Nakagawa’s RubyCocoa Tutorials

I recently translated these tutorials from Japanese.

A much longer list of tutorials can be found here.

MacRuby Random Number Generator Tutorial

There is a very popular book out there for learning Cocoa called Cocoa® Programming for Mac® OS X, Third Edition by Aaron Hillegass. A number of people have suggested that, even if I want to program using RubyCocoa or MacRuby and don’t care much for learning the Objective-C language, you can learn a lot by going through the Hillegass book and trying to turn the examples into Ruby.

Since my own Ruby writing skills are still relatively elementary and my Objective-C reading skills are poor to say the least, I’m not very confident I’ll be able to do this for all examples I come across in that book. However, I want to pass on whatever I can do to others who may be as new to this as I am.

For this purpose I made a new MacRuby screencast tutorial for the first example of the Hillegass book called RandomApp.1 I would recommend doing the earlier MacRuby tutorial first but this tutorial repeats many of the same basic steps involving the connecting of outlets and actions.

MacRuby Random Number Generator Screencast (6.23MB 3m:50s)

Here is the completed XCode project for you to play with:

MacRuby Random Number Generator XCode Project

See also a RubyCocoa version of this Hillegass example which doesn’t need Interface Builder over at RubyCocoa resources.

UPDATE: The credits at the end make it seem like this is a RubyCocoa example, which it isn’t. This is a MacRuby example. This works for RubyCocoa as well but you need a few modifications to the code.

  1. You can find this in Chapter 2, beginning page 9. []

Browser Tutorial #1: Creating a Simple Web Browser in RubyCocoa

The Webkit framework has made it ridiculously easy to create a browser as part of any OS X application and there are tutorials all over the web to show how this can be done in Cocoa applications. For the basics, it doesn’t require any code at all, so it comes as no surprise that this as easy in RubyCocoa applications as in other languages.

In the first of several tutorials (only some of which will be screencasts), I will show the basics of creating an extremely simple browser and then we’ll move on to add some Ruby code and other features in later episodes.

For a more advanced XCode project to play with that incorporates more features than we will get to in these tutorials, feel free to dive into the example that comes with RubyCocoa, which is located in the /Developer/Examples/RubyCocoa/MiniBrowser folder.

RubyCocoa Webkit Tutorial #1

RubyCocoa Webkit Tutorial XCode Project

Tutorial #1 Summary

In the first screencast I will go through the following simple steps:

1. Add controls for the browser, including the WebViewer control.
2. Connect these controls to Webkit methods associated with the WebViewer
3. Fix autosizing for the address field and the WebView browser component.
4. Add the Webkit framework to the Linked Frameworks folder.
5. Activate the “New” file menu item.

UPDATE: jp_tix on #ruby-osx kindly pointed out that you don’t have to change a Custom View into a WebView in Interface Builder, but can drag a WebView directly from the Library. I didn’t notice the availability of this control. Thanks! You can find this in the library and it looks like this:

Interface BuilderScreenSnapz002.jpg

Adding a Check Updates Feature for RubyCocoa and MacRuby

Do you want to have a nice “Check for Updates…” feature in your new application without having to code it yourself? The Sparkle module provides a wonderful and free way of implementing this in your application.

This tutorial is essentially a modified version of the tutorial I created for using the Sparkle updates module on my Applescript Studio weblog. The steps for getting a nice “Check Updates…” feature in your RubyCocoa application using Sparkle are pretty much the same with one small change that seems to be specific to the current version of Sparkle.

The tuturial below should technically also work for MacRuby but, unlike RubyCocoa, MacRuby requires GC (Garbage Collection). There is a GC version of Sparkle which came with the release I used but it does not appear to be functional. When it does work, you’ll be able to follow the steps below with one modification in step 2, which is noted below.

(Continued)

MacRuby Tutorial

There is a nice and simple tutorial that can be found towards the end of this page at the MacRuby site. I gave it a try but also wanted to test the way that MacRuby can seamlessly convert to and from Cocoa strings without conversion.

In the following screencast I create a simple MacRuby application that first goes through the tutorial on the MacRuby site. This tutorial simply outputs a line to the console when a button is clicked. I then add two text fields and change the action of the button to take the text from the input field and output it with some added text to the output field.

MacRuby Tutorial (4 minutes and 4 seconds, 20.5MB)

My voice is a bit off so I put some simple commentary as subtitles rather than adding a voiceover.

You can also download the XCode project created by this screencast:

MacRuby Tutorial XCode Project

The only real code by the end of the screen cast is the code for the controller class, which is:

class MyController < NSWindowController
  ib_outlet :button, :inputfield, :outputfield
  ib_action :clicked do  |sender|
    @outputfield.stringValue="You entered: "+@inputfield.stringValue
  end
end

UPDATE: Got some recommendations from lrz including changing setStringValue to simply StringValue. May redo this screencast and also use fixed rather than moving screen. I’m still new to the screencast world. I’ll figure things out.

UPDATE: I have completely redone the screencast and added a note at the end showing an easier way of changing the string value of a text field. Instead of setStringValue, you can use stringValue= as in the code above. The original line is below:

@outputfield.setStringValue("You entered: "+@inputfield.stringValue)

UPDATE: One more error pointed out by lrz: in the added note at the end of the tutorial I used “StringValue=” instead of setStringValue but this should be “stringValue” with a small s. I have updated this in the code, the above posting, and the screencast.

Translation of Satoshi Nakagawa’s RubyCocoa Tutorials

Satoshi Nakagawa, a contributor to RubyCocoa’s development and the creator of the IRC chat client LimeChat has some great resources online that can help with learning RubyCocoa. They are mostly in Japanese so there are many who won’t be able to benefit from them.

I especially liked some really clear and simple tutorials for RubyCocoa that Mr. Nakagawa has made available.

I am almost finished with a translation of these Japanese language tutorials into English. You can read the English translation of these tutorials here:

Satoshi Nakagawa’s RubyCocoa Tutorials

Thanks to Mr. Nakagawa for writing the tutorials.

UPDATE: I finished the translation this evening. Enjoy

Missing MacRuby Project Template

After installing MacRuby 0.2 I tried to follow the tutorial available online. However, I discovered XCode 3.1 didn’t display the “MacRuby Application” project template the way the tutorial indicated it should.

I spoke to MacRuby developer lrz on #ruby-osx yesterday, and he was kind enough to send me an empty MacRuby project that can be used if you are unable to get the project template to work. Note that when you rename the project you need to search through and replace all the old references to the project name with your own project name. You can download this blank project here:

New MacRuby Project

Today, however, user lachie on #ruby-osx was able to find a more direct fix for this problem. If you have XCode 3.1 and have trouble finding the project template he found that all you had to do was move the “MacRuby Application” folder from here:

/Library/Application Support/Developer/3.0/Xcode/Project Templates/Application/MacRuby Application

In my case this looked like this:
FinderScreenSnapz002.jpg

to here:

/Users/YOUR_USER_NAME/Library/Application Support/Developer/Shared/Xcode/Project Templates/Application/MacRuby Application

Which in my case looked like:

FinderScreenSnapz001.jpg

And when next booted up XCode 3.1 I found the “User Templates” category which listed the new option of “MacRuby Application” looking like this:

XcodeScreenSnapz001.jpg

I’ll update this posting if I hear of any better ways of doing this or find that new releases of MacRuby remove the need to do this. My warm thanks to both lrz and lachie.

New Ruby and Cocoa Google Group

There is a good mailing list out there for RubyCocoa and a development mailing list for MacRuby.

In order to create an online forum for discussing development with Ruby and Cocoa I created a new Google Group where people working with MacRuby, RubyCocoa, or RubyObjC can gather to discuss Ruby and Cocoa development issues:

Ruby and Cocoa Google Group

I just created this yesterday but I hope it will turn into a useful resource for anyone working in this environment. I’m glad to see we already have on board lrz who is a developer of MacRuby, and hope it will grow to include others in the Ruby and Cocoa development world.

New Ruby and Cocoa Weblog

I’m not a programmer by trade. This weblog is maintained by a student of history who likes to tinker with coding on the side for OS X and some online websites. For a few weeks I have tried to rewrite an old OS 9 project using AppleScript Studio and posted some tips and tutorials on a separate weblog dedicated to that environment. My frustrations with the applescript language, however, and my appreciation for the beauty of Ruby has led me to start over and try to learn how to combine the power of the Ruby language with the ease of GUI design in Interface Builder and the broader world of Cocoa development.

I am no expert on Cocoa, XCode, Interface Builder, Ruby, or either RubyCocoa or MacRuby so this weblog is a window into one newbie’s attempt to learn all of them. I’ll post tips I found useful, tutorials I come across and one’s I create on my own. I will also collect useful links related to RubyCocoa and MacRuby and you can find them in the blogroll located in the sidebar.

I will start off trying to learn RubyCocoa but I’m excited by the new MacRuby project which is aiming to overcome some apparent weaknesses with the former. I may eventually shift to the latter as it becomes more mature and support for it grows.

There is also another Objective-C bridge RubyObjC, by Tim Burks who now focuses on a Lisp and Ruby influenced language called Nu but I won’t be mentioning that much here.

I am also interested in welcoming others to join as contributors to this weblog if you are interested in Ruby and Cocoa development and want to make postings useful to others who are new to the topic.