Download - Developing Cocoa Applications with macRuby
![Page 1: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/1.jpg)
Brendan G. Lim@brendanlim
Developing CocoaApplications With
MacRuby
Saturday, February 5, 2011
![Page 2: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/2.jpg)
• Objective-C & Cocoa
• RubyCocoa
• MacRuby
• Live Coding
• HotCocoa
Outline
Saturday, February 5, 2011
![Page 3: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/3.jpg)
• Object-oriented extensions to C
• Strongly typed
• Like Ruby, influenced by Smalltalk
• Primarily used for Mac OS X and iOS
Objective -C
Saturday, February 5, 2011
![Page 4: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/4.jpg)
• High-level API for Mac OS X
• Set of frameworks
• Includes FoundationKit, AppKit, etc.
• Apps typically built using tools like XCode and Interface Builder
Cocoa
Saturday, February 5, 2011
![Page 5: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/5.jpg)
Why make desktop apps?
Saturday, February 5, 2011
![Page 6: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/6.jpg)
Different Paradigm
Saturday, February 5, 2011
![Page 7: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/7.jpg)
Mac App Store
1. Build MacRuby application2. Submit to App Store4. Profit
Saturday, February 5, 2011
![Page 8: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/8.jpg)
Why Ruby insteadof Objective-C?
Saturday, February 5, 2011
![Page 9: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/9.jpg)
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.6RubyCocoa, RubyGems, Rails
2009 Mac OS X 10.6 Ruby 1.8.7RubyCocoa, RubyGems, Rails
2011 Mac OS X 10.7 Ruby 1.9.x?MacRuby? RubyCocoa, RubyGems, Rails
Saturday, February 5, 2011
![Page 10: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/10.jpg)
object.method(param)
[object method:param];
=
Ruby vs Objective-C
Saturday, February 5, 2011
![Page 11: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/11.jpg)
array = []
NSMutableArray *array = [[NSMutableArray alloc] init];
=
Ruby vs Objective-C
Saturday, February 5, 2011
![Page 12: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/12.jpg)
“ string”.strip
[@“ string” stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
=
Ruby vs Objective-C
Saturday, February 5, 2011
![Page 13: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/13.jpg)
dictionary = {“key1” => “value1”, “key2” => “value2”}
NSArray *keys = [NSArray arrayWithObjects:@”key1”,@”key2”];NSArray *data = [NSArray arrayWithObjects:@”value1”,@”value2”];NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
=
Ruby vs Objective-C
Saturday, February 5, 2011
![Page 14: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/14.jpg)
+
Saturday, February 5, 2011
![Page 15: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/15.jpg)
• 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
RubyCocoa
Saturday, February 5, 2011
![Page 16: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/16.jpg)
OSX::NSNotificationCenter.defaultCenter.addObserver_selector_name_object( self, :window_moved, "NSWindowDidMoveNotification", nil)
[[NSNotificationCenter defaultCenter] addObserver:self, selector:@selector(windowMoved:) name:”NSWindowDidMoveNotification” object:nil];
=
RubyCocoa vs Objective-C
Saturday, February 5, 2011
![Page 17: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/17.jpg)
So, Why Not RubyCocoa?
(besides it looking gross)
Saturday, February 5, 2011
![Page 18: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/18.jpg)
• It’s a bridge!
• Two runtimes, GCs, etc.
• Object conversions
• Syntax doesn’t feel like idiomatic Ruby
• It’s getting replaced
Why Not RubyCocoa?
Saturday, February 5, 2011
![Page 19: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/19.jpg)
+
Saturday, February 5, 2011
![Page 20: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/20.jpg)
• 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
MacRuby
Saturday, February 5, 2011
![Page 21: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/21.jpg)
Object
String
Number
Array
Hash
NSObject
NSMutableString
NSNumber
NSMutableArray
NSMutableDictionary
MacRuby
Saturday, February 5, 2011
![Page 22: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/22.jpg)
Objects
Classes
Methods
Objective-C
Objective-C
Objective-C
MacRuby
Saturday, February 5, 2011
![Page 23: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/23.jpg)
>> s = “magicruby”=> “magicruby”
>> s.class=> String
>> s.class.ancestors=> [String,NSMutableString,NSString,Comparable,NSObject,Kernel]
>> s.upcase=> “MAGICRUBY”
>> s.uppercaseString=> “MAGICRUBY”
MacRuby
Saturday, February 5, 2011
![Page 24: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/24.jpg)
>> NSString.new(“magicruby”)=> “magicruby”
>> NSString.stringWithString(“magicruby”)=> “magicruby”
>> NSString.alloc.initWithString(“magicruby”)=> “magicruby”
MacRuby
Saturday, February 5, 2011
![Page 25: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/25.jpg)
>> a = []=> []
>> a.class=> Array
>> a.class.ancestors=> [Array,NSMutableArray,NSArray,Enumerable,NSObject,Kernel]
>> a << “MagicRuby”=> [“MagicRuby”]
MacRuby
Saturday, February 5, 2011
![Page 26: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/26.jpg)
MacRuby vs Objective-C-(id) tableView:(NSTableView *)tableView objectValueForColumn:(NSTableColumn *)column row:(int)rowIndex { .. }
Saturday, February 5, 2011
![Page 27: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/27.jpg)
MacRuby vs Objective-C-(id) tableView:(NSTableView *)tableView objectValueForColumn:(NSTableColumn *)column row:(int)rowIndex { .. }
def tableView(tableView objectValueForColumn:column
row:rowIndex)end
Saturday, February 5, 2011
![Page 28: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/28.jpg)
Interface Builder Outlets & Actions
MacRuby vs Objective-C
!
Saturday, February 5, 2011
![Page 29: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/29.jpg)
NSString *myString;@property(nonatomic,retain) IBOutlet NSString *myString;
attr_accessor :myString
Interface Builder Outlets
=
# Interface
MacRuby vs Objective-C
Saturday, February 5, 2011
![Page 30: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/30.jpg)
def myAction(sender)...
end
Interface Builder Actions
=
-(IBAction) myAction:(id)sender { ... }# Implementation
MacRuby vs Objective-C
Saturday, February 5, 2011
![Page 31: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/31.jpg)
MacRuby - Gem Support
• sudo macgem install awesome_gem
• Not all gems supported right now
Saturday, February 5, 2011
![Page 32: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/32.jpg)
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
![Page 33: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/33.jpg)
• Any Ruby testing framework instantly becomes an Objective-C testing framework
• Test::Unit
• RSpec
• etc...
Testing MacRuby
Saturday, February 5, 2011
![Page 34: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/34.jpg)
What tools will webe using?
Saturday, February 5, 2011
![Page 35: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/35.jpg)
Xcode
Saturday, February 5, 2011
![Page 36: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/36.jpg)
Interface Builder
Saturday, February 5, 2011
![Page 37: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/37.jpg)
Instruments
Saturday, February 5, 2011
![Page 38: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/38.jpg)
Let’s build a MacRuby app
Saturday, February 5, 2011
![Page 39: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/39.jpg)
• 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
HotCocoa
Saturday, February 5, 2011
![Page 40: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/40.jpg)
win = NSWindow.alloc.initWithContentRect([10,20,300,300], styleMask: (NSTitleWindowMask | NSCloseableWindowMask | NSMiniatureizableWindowMask | NSResizeableWindowMask)
win = window :frame => [10,20,300,300]
=
HotCocoa
Saturday, February 5, 2011
![Page 41: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/41.jpg)
HotCocoa
sudo macgem install hotcocoa
hotcocoa NAME_OF_APP
Saturday, February 5, 2011
![Page 42: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/42.jpg)
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 endend
Application.new.start
Hello World in HotCocoa
Saturday, February 5, 2011
![Page 43: Developing Cocoa Applications with macRuby](https://reader034.vdocument.in/reader034/viewer/2022052619/5555abd2d8b42a52568b4946/html5/thumbnails/43.jpg)
http://macruby.orghttp://bit.ly/macruby-getting-started
http://bit.ly/macruby-exampleshttp://bit.ly/tdd-macruby
Questions?
MacRuby in Actionhttp://manning.com/lim
Saturday, February 5, 2011