Your First RubyObjC Application
You have just built your first Cocoa application with RubyObjC. You should now see a Cocoa window like the one above. Does it look familiar? If so, say hello. The console that you see is irb, and it is built into every RubyObjC application. It's available optionally and is enabled by some special code in your application's main.rb file. You can read more about that in the RubyObjC documentation. For now, let's look at some things you can do with it.
Did you type puts "hello" like I suggested? Maybe you also tried entering some other Ruby code there too. If you find something that doesn't work, let me know. My goal is to make sure that that's a complete Ruby interpreter.
Now type ObjC::NSApp. That will get you a Ruby wrapper for the application's NSApplication.sharedApplication object. If you set it to a variable, you can play with it. Or just try this: ObjC::NSApp.methods.sort. You'll get an enormously long list of methods. Some of these are Ruby methods, but most of them are from the Objective-C runtime. Near the end there's one called "windows". Let's call it: ObjC::NSApp.windows (you can use the arrows to page back to prior commands). What did you get? An array? Actually you got a Ruby wrapper for a Cocoa array class. But you can still pretend it's an array. Now do this: ObjC::NSApp.windows.length. You should get a "1" in response. Now do this: ObjC::NSApp.windows[0].setTitle_ "I rule!". With RubyObjC, you do.
You can do a lot more than that. With power, you also get knowledge. You can use RubyObjC to look deep into the Objective-C runtime and uncover its secrets. Here's an example. Type c = ObjC::Class.find("NSWindow") at the console. You'll get a Ruby wrapper for the Objective-C NSWindow class. Now ask it about its instance methods: c.imethods. You got another huge list, didn't you? Let's pick one near the end of the list.
m = c.get_imethod("_setWantsToBeOnMainScreen:")
Now that you have the method, you can find out some things about it. m.argument_count (hey! do you know why it's 3 and not 1?... you will!) Then try this: m.signature. That tells you a lot. It's the Objective-C runtime's encoding of the argument and return types for this method. It returns void (thus the "v"), and the method is sent to an object ("@"), it is specified by a selector (":"), and expects a character as it's single method argument. You can learn more about these codes on Apple's Developer Connection web site. Look there for a detailed description of the Objective-C type encodings.
While you're in the console, type "raw". That's short for "RandomAppWindow" and will bring up a familiar sight if, like me, you learned Cocoa from Aaron Hillegass' book. On the surface this looks like his introductory Cocoa app, but underneath it's all Ruby. If you want to see how it's done, look at the random.rb file in your application directory. If you don't need it, you can safely delete the file. (But not before you learn how to use the console to make live changes to that little RandomApp window!)
The RubyObjC documentation describes some of this in more detail, and I plan to post more expositions in the future. Let me know if something doesn't seem right, and happy programming!
p.s. When you're finished with the interpreter, type this: ObjC::NSApp.windows[0].close. Bye!