Skip to content

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. []

3 Comments

  1. fred b. wrote:

    Hi Konrad, many thx for your excellent idea to follow Aaron Hillegass book, I am trying to learn in parallel Cocoa/Obj-c and MacRuby. Any reason why you chose to subclass MyController from NSWindowController? I tried to stick to Aaron example, and subclassed a Foo class from NSObject, and also does the job:

    class Foo < NSObject
    attr_accessor :messages
    def seed(sender)
    t = Time.now
    srand t.to_i
    @messages.stringValue=”Generated seed using time: “+t.to_s
    end
    def generate(sender)
    @messages.stringValue=(rand(100)+1).to_s
    end
    def awakeFromNib @messages.stringValue=”Click seed first…”
    end
    end

    Saturday, October 25, 2008 at 9:22 am | Permalink
  2. K. M. Lawson wrote:

    Hello Fred, I am not sure why I did that. I think NSObject is what I would I have done if I was thinking it through.

    Thanks!

    Sunday, October 26, 2008 at 4:02 am | Permalink
  3. Michael Leung wrote:

    These MacRuby screencasts are awesome. Keep ‘em coming!

    Friday, October 31, 2008 at 4:15 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*