1. Developing Cocoa
Applications With
MacRuby
Brendan G. Lim
@brendanlim
brendan@intridea.com
Saturday, February 5, 2011
2. Outline
• Objective-C & Cocoa
• RubyCocoa
• MacRuby
• Live Coding
• HotCocoa
Saturday, February 5, 2011
3. Objective -C
• Object-oriented extensions to C
• Strongly typed
• Like Ruby, influenced by Smalltalk
• Primarily used for Mac OS X and iOS
Saturday, February 5, 2011
4. Cocoa
• High-level API for Mac OS X
• Set of frameworks
• Includes FoundationKit, AppKit, etc.
• Apps typically built using tools like XCode
and Interface Builder
Saturday, February 5, 2011
9. Apple Loves Ruby
2002 Mac OS X 10.2 Ruby 1.6.7
2005 Mac OS X 10.4 Ruby 1.8.2
2007 Mac OS X 10.5 Ruby 1.8.6
RubyCocoa, RubyGems, Rails
2009 Mac OS X 10.6 Ruby 1.8.7
RubyCocoa, RubyGems, Rails
2011 Mac OS X 10.7 Ruby 1.9.x?
MacRuby? RubyCocoa, RubyGems, Rails
Saturday, February 5, 2011
10. Ruby vs Objective-C
object.method(param)
=
[object method:param];
Saturday, February 5, 2011
11. Ruby vs Objective-C
array = []
=
NSMutableArray *array =
[[NSMutableArray alloc] init];
Saturday, February 5, 2011
12. Ruby vs Objective-C
“ string”.strip
=
[@“ string” stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]]
Saturday, February 5, 2011
15. RubyCocoa
• Bridge between Objective-C and Ruby
• Manipulate Objective-C objects using Ruby
• Write Cocoa apps in Ruby
• Runs on Ruby 1.8
• Ships with OSX Leopard
Saturday, February 5, 2011
20. MacRuby
• Implementation of Ruby 1.9 that runs on top
the Objective-C runtime
• Open sourced and supported by Apple
• Replacing RubyCocoa
• Objects are peers with no translation layer
Saturday, February 5, 2011
21. MacRuby
Object NSObject
String NSMutableString
Number NSNumber
Array NSMutableArray
Hash NSMutableDictionary
Saturday, February 5, 2011
30. MacRuby vs Objective-C
Interface Builder Actions
# Implementation
-(IBAction) myAction:(id)sender { ... }
=
def myAction(sender)
...
end
Saturday, February 5, 2011
31. MacRuby - Gem Support
• sudo macgem install awesome_gem
• Not all gems supported right now
Saturday, February 5, 2011
32. MacRuby - Objective-C
Frameworks & Libraries
• Libraries must have garbage collection support
• Libraries must be turned into bundles
• Frameworks can easily be included
Saturday, February 5, 2011
33. Testing MacRuby
• Any Ruby testing framework instantly becomes
an Objective-C testing framework
• Test::Unit
• RSpec
• etc...
Saturday, February 5, 2011
38. Let’s build a MacRuby app
Saturday, February 5, 2011
39. HotCocoa
• Created by Rich Kilmer
• Ruby layer that sits on top of Cocoa, etc.
• Use Ruby to easily create user interfaces
• Used to be included with MacRuby
• Now available as a gem
Saturday, February 5, 2011
42. Hello World in HotCocoa
require ‘hotcocoa’
class Application
include HotCocoa
def start
application :name => "Hello" do |app|
app.delegate = self
window :frame => [500,500,200,100], :title => "Hello" do |win|
win << label(:text => "Hello World",:layout => {:start => false})
win.will_close { exit }
end
end
end
end
Application.new.start
Saturday, February 5, 2011
43. Questions?
MacRuby in Action
http://manning.com/lim
http://macruby.org
http://bit.ly/macruby-getting-started
http://bit.ly/macruby-examples
http://bit.ly/tdd-macruby
Saturday, February 5, 2011