Coding under the Hammer
MacRuby – First Impression
What not Objective-C !!! whats going on, I hear you cry. Well I thought I would mix things up a bit, after all it has got Mac in the name.
I am a firm believer that learning a new language, makes you better at developing with the ones you already know. Different languages often attack problems from different angles, depending on the language’s capabilities (built in classes etc) and common design patterns. Im sure people that are new to iPhone/Mac programming, where shocked about how often they had to type the word delegate.
So why Ruby ?
I admit it, I have played with Ruby before and the fact that I quite like it did help its cause, but that isn’t the main reason. The main reason is the fact you can use it for almost anything. You can use it as a CGI script on the web, you can build Web applications using Ruby On Rails, you can do shell scripting to automate tasks, and obviously you can build desktop applications.
So what is MacRuby ?
MacRuby is Ruby 1.9 built on top of the Objective-C runtime, which allows you to build Mac applications using Ruby (Its actually an Apple run project). The real sexy bit is you can call any Objective-C method directly using Ruby, meaning that you have access to all of OS X functionality. What is even more surprising, is that you can use Interface builder to put together your UI (well it was a surprise to me anyway).
Unfortunately MacRuby doesn’t currently work on the iPhone as it needs the garbage collector, but at some point this may change.
Interface Builder
So if your using interface builder, your need to declare IBOutlets so you can access a UI elements from in your code.
Objective-C
IBOutlet NSTableView *tableView;
In Ruby you don’t actually type cast your variables, so the only way to achieve this is to declare an accessor to that variable.
attr_accessor :tableView
In addition to IBOutlets your also need to declare IBActions for button presses etc.
Objective-C
- (IBAction)pressMe:(id)sender{
}
In Ruby you need to define a method with the sender parameter
def pressedButton(sender)
end
Calling Objective-C methods from Ruby
As you are building Mac application with Ruby, you will want to access the vast array of APIs available in Mac OS X. These are (mostly) written in Objective-C. One common task is creating a string:
Objective-C
NSString *string = [NSString stringWithString:@"Test string"];
In Ruby you do the following. Notice that there is no pointers and you do not need to declare a type.
string = NSString.alloc.initWithString("Test String")
*As Ted Wise correctly points out in the comments, you can create a NSMutableString by do the following piece of Ruby code, but I thought the string example was a nice and concise one to use.
string = "Test String"
From the above code snippets you will also notice that like most languages, Ruby doesn’t “stagger” its variables like Objective-C.
So how would you call the following Objective-C code snippet in Ruby ?
NSAlert *alert = [NSAlert alertWithMessageText:@"title"
defaultButton:@"ok"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@"This is an alertview"];
[alert runModal];
Well quite easily actually, as MacRuby actually adds this syntax to Ruby, so it would look like the following:
alert = NSAlert.alertWithMessageText("Title", defaultButton:"OK", alternateButton:nil, otherButton:nil, informativeTextWithFormat:"This is an alertview")
alert.runModal()
Easy enough ?
Conclusion (of my first impressions)
This post quite literally touches the surface of MacRuby, but I think it is an exciting technology. Ruby is really good for getting stuff done quickly, as the language does a lot of heavy lifting for you. By doing the Model part of the MVC in Ruby, you could in theory share the code across applications on the desktop and the web, which can’t be bad.
If your new to the Mac and don’t know ether Objective-C or Ruby, I would still learn Objective-C first if I am being honest. Learning Cocoa is hard enough without all the examples being in another language. But if your new to the Mac and already know Ruby, it might be a good way to start mac development.
| Print article | This entry was posted by Spencer MacDonald on March 12, 2010 at 2:51 pm, and is filed under Mac. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 5 months ago
Great post! But “MacRuby actually adds this syntax to Ruby” isn’t very true. As far as I know, named parameter (actually a hash of params) is a Ruby 1.9 feature, nothing to do with MacRuby. Once again, AFAIK.
about 5 months ago
This Objective-C code:
NSString *string = [NSString stringWithString:@"Test string"];
Has the Ruby equivalent:
string = “Test String”
Strings in MacRuby are NSMutableStrings, which are subclasses of NSString. You don’t need to alloc or construct them.
about 5 months ago
Yeah I know
Just it is an easy example to give, will point that out in the post to make it clearer.
about 5 months ago
Great post, very interesting stuff.
I know Ruby has a lot of it’s own frameworks (Not the Obj-c ones) is it possible to build a mac app and mix both the ruby frameworks and Obj-c ones?
about 5 months ago
Hi Duncan,
Yes you can. When you deploy your application you include the MacRuby Framework as an embedded framework, the same way you would do with any Mac OS framework that isn’t included with the OS e.g. Growl, Sparkle etc.
Doing this means that users of your application do not need to have installed the MacRuby SDK to use it, and that you can have access to Ruby functionality as long as you embed the relevant source in the build.