macruby to the max
TRANSCRIPT
MACRUBY Brendan G. Lim@brendanlim
WHO HERE HAS AMAC?
WHO HERE HASWORKED WITH
OBJECTIVE-C?
DEVELOPEDOSX OR IPHONE APPS?
OBJECTIVE-C
• Object-oriented extensions to C
• Strongly typed
• Like Ruby, influenced by Smalltalk
• Primary language for Cocoa
• Garbage collection (2.0)
• 32 and 64-bit support
COCOA
• Set of object-oriented frameworks
• Used for writing Max OSX and iPhone OS apps
• Includes FoundationKit, and AppKit
• Typically built using tools like XCode and Interface Builder
CORE FOUNDATION
• Low level libraries that ship with Mac OSX
• Used for passing primitive types to OSX C routines
• Most routines follow ‘CF’ naming convention
• Open-Sourced by Apple as CF-Lite
WHY RUBY INSTEAD OF OBJECTIVE-C?
RUBY VS OBJECTIVE-C
object.method(param)
[object method:param];
=
RUBY VS OBJECTIVE-C
person.set_name(“john”, “doe”)
[person setNameWithFirst:@”john” withLast:@”doe”];
=
RUBY VS OBJECTIVE-C
array = []
NSMutableArray *array = [[NSMutableArray alloc] init];
=
RUBY VS OBJECTIVE-C
“ string”.strip
[@“ string” stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
=
RUBY VS OBJECTIVE-C
dictionary = {“key1” => “value1”, “key2” => “value2”}
NSArray *keys = [NSArray arrayWithObjects:@”key1”,@”key2”];NSArray *data = [NSArray arrayWithObjects:@”value1”,@”value2”];NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
=
RUBYCOCOA• Mac OSX framework
• Bridge between Objective-C and Ruby
• Can manipulate Objective-C objects using Ruby
• Write Cocoa apps in Ruby
• Runs on Ruby 1.8
• Ships with OSX Leopard
RUBYCOCOA VS OBJECTIVE-C
setObject_forKey_(iphone, phone)
[setObject:iphone forKey:phone];
=
SO WHY NOTRUBYCOCOA?
WHY NOT RUBYCOCOA
• Performance
• Two runtimes
• Two garbage collectors
• Object conversions
• Syntax doesn’t feel like Ruby
+
MACRUBY
• Impl. of Ruby 1.9 that runs on the Objective-C runtime
• Open sourced by Apple
• Originally created by Laurent Sansonetti
• Replacing RubyCocoa
• Objects are peers with no translation layer
• Multi-core (Grand Central Dispatch)
• HotCocoa Support
MACRUBY
MACRUBYObject
String
Number
Array
Hash
NSObject
NSMutableString
NSNumber
NSMutableArray
NSMutableDictionary
MACRUBY
Objects
Classes
Methods
Objective-C
Objective-C
Objective-C
MACRUBY>> s = “rubyconf”=> “rubyconf”
>> s.class=> NSMutableString
>> s.class.ancestors=> [NSMutableString,NSString,Comparable,NSObject,Kernel]
>> s.upcase=> “RUBYCONF”
>> s.uppercaseString=> “RUBYCONF”
MACRUBY
>> NSString.new(“rubyconf”)=> “rubyconf”
>> NSString.stringWithString(“rubyconf”)=> “rubyconf”
>> NSString.alloc.initWithString(“rubyconf”)=> “rubyconf”
MACRUBY>> a = []=> []
>> a.class=> NSMutableArray
>> a.class.ancestors=> [NSMutableArray,NSArray,Enumerable,NSObject,Kernel]
>> a << “RubyConf”=> [“RubyConf”]
MACRUBY VS OBJECTIVE-C
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {}
MACRUBY VS OBJECTIVE-C
def locationManager(manager didUpdateToLocation:newLocation
fromLocation:oldLocation)end
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {}
MACRUBY VS OBJECTIVE-C
def locationManager(manager didUpdateToLocation:newLocation
fromLocation:oldLocation)end
>> locationManager(manager didUpdateToLocation:newLocation, fromLocation:oldLocation)
MACRUBY VS OBJECTIVE-C
def locationManager(manager didUpdateToLocation:newLocation
fromLocation:oldLocation)end
>> locationManager(manager :didUpdateToLocation => newLocation, :fromLocation => oldLocation)
MACRUBY VS OBJECTIVE-C
NSString *myString;@property(nonatomic,retain) IBOutlet NSString *myString;
attr_accessor :my_string
Interface Builder Outlets
=
# Interface (header file)
MACRUBY VS OBJECTIVE-C
-(IBAction) myAction:(id)sender;
def my_action(sender)...
end
Interface Builder Actions
=
# Interface
-(IBAction) myAction:(id)sender { ... }# Implementation
WAIT, WHAT ABOUT MACRUBY FOR IPHONE/IPAD?
MACRUBY
Xcode
MACRUBY
Interface Builder
MACRUBY
Instruments
MACRUBY
Let’s build something ...
TDD IN MACRUBY
• Good supports for tests
• Any Ruby testing framework instantly becomes an Objective-C testing framework
• Test::Unit
• RSpec
• Cucumber
• etc...
HOTCOCOA
• Use Ruby to build a UI without Interface Builder
• Developed by Rich Kilmer
• MacRuby 0.5 now supports HotCocoa
• Packaged as a gem in MacRuby 0.5
MACRUBY VS HOTCOCOA
win = NSWindow.alloc.initWithContentRect([10,20,300,300],:styleMask => (NSTitleWindowMask | NSCloseableWindowMask | NSMiniatureizableWindowMask | NSResizeableWindowMask)
win = window :frame => [10,20,300,300]
=
HELLO WORLD IN HOTCOCOA
require ‘rubygems’require ‘hotcocoa’include HotCocoaapplication do |app|win = window :size => [100,50]b = button :title => ‘Hello’b.on_action { puts ‘World!’ }win << b
end
MACRUBY
Very exciting future
MACRUBY
It’s Ruby for Cocoa Apps!
MACRUBY
http://macruby.orghttp://macruby.org/hotcocoa.html
http://bit.ly/macruby-getting-startedhttp://bit.ly/macruby-examples
http://bit.ly/tdd-macrubyhttp://github.com/masterkain/macruby
Questions?