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.
8 Comments
Way cool! Ruby is really making inroads, isn’t it?
Great, this second version is better!
Just a small note, the real method to be called here should be stringValue=, which sounds more rubyish. The fact that StringValue= (with a capital letter) works is pure luck, because of a bug in MacRuby
Thanks for the screencast, and keep up the very good work!
Doh, another error. Ok, I updated this in the posting and in the XCode project download . I also made a small adjustment in the end of the screencast and it is exporting now. It should be uploaded in an hour or so.
My son is interested in learning to program, and specifically for his new iTouch. I’d rather he learn (and I teach him) Ruby instead of Objective-C. Can one use MacRuby on the latest iPhone SDK?
This is really sweet. So nice to have something to help with those first few moments with InterfaceBuilder. Thank you.
Keep them coming. This is a great screencast, and there is definitely a shortage of them.
I always got the message “`ClickTestButton’: undefined method `stringValue’ for nil:NilClass (NoMethodError)”
What am I doing wrong?
This is not the MacRuby language, it’s RubyCocoa.
In the MacRuby language you don’t need to add ‘ < NSWindow Controller’ and instead of ‘ib_outlet’ you use ‘attr_writer’ and ‘attr_accessor’
You also don’t use ‘ib_action :clicked …’ but ‘def clicked(sender)’
Post a Comment