macruby to the max

42
MAC RUBY Brendan G. Lim @brendanlim [email protected]

Upload: brendan-lim

Post on 29-Jan-2018

8.905 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: MacRuby to The Max

MACRUBY Brendan G. Lim@brendanlim

[email protected]

Page 2: MacRuby to The Max

WHO HERE HAS AMAC?

Page 3: MacRuby to The Max

WHO HERE HASWORKED WITH

OBJECTIVE-C?

Page 4: MacRuby to The Max

DEVELOPEDOSX OR IPHONE APPS?

Page 5: MacRuby to The Max

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

Page 6: MacRuby to The Max

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

Page 7: MacRuby to The Max

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

Page 8: MacRuby to The Max

WHY RUBY INSTEAD OF OBJECTIVE-C?

Page 9: MacRuby to The Max

RUBY VS OBJECTIVE-C

object.method(param)

[object method:param];

=

Page 10: MacRuby to The Max

RUBY VS OBJECTIVE-C

person.set_name(“john”, “doe”)

[person setNameWithFirst:@”john” withLast:@”doe”];

=

Page 11: MacRuby to The Max

RUBY VS OBJECTIVE-C

array = []

NSMutableArray *array = [[NSMutableArray alloc] init];

=

Page 12: MacRuby to The Max

RUBY VS OBJECTIVE-C

“ string”.strip

[@“ string” stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]

=

Page 13: MacRuby to The Max

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];

=

Page 14: MacRuby to The Max

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

Page 15: MacRuby to The Max

RUBYCOCOA VS OBJECTIVE-C

setObject_forKey_(iphone, phone)

[setObject:iphone forKey:phone];

=

Page 16: MacRuby to The Max

SO WHY NOTRUBYCOCOA?

Page 17: MacRuby to The Max

WHY NOT RUBYCOCOA

• Performance

• Two runtimes

• Two garbage collectors

• Object conversions

• Syntax doesn’t feel like Ruby

Page 18: MacRuby to The Max

+

MACRUBY

Page 19: MacRuby to The Max

• 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

Page 20: MacRuby to The Max

MACRUBYObject

String

Number

Array

Hash

NSObject

NSMutableString

NSNumber

NSMutableArray

NSMutableDictionary

Page 21: MacRuby to The Max

MACRUBY

Objects

Classes

Methods

Objective-C

Objective-C

Objective-C

Page 22: MacRuby to The Max

MACRUBY>> s = “rubyconf”=> “rubyconf”

>> s.class=> NSMutableString

>> s.class.ancestors=> [NSMutableString,NSString,Comparable,NSObject,Kernel]

>> s.upcase=> “RUBYCONF”

>> s.uppercaseString=> “RUBYCONF”

Page 23: MacRuby to The Max

MACRUBY

>> NSString.new(“rubyconf”)=> “rubyconf”

>> NSString.stringWithString(“rubyconf”)=> “rubyconf”

>> NSString.alloc.initWithString(“rubyconf”)=> “rubyconf”

Page 24: MacRuby to The Max

MACRUBY>> a = []=> []

>> a.class=> NSMutableArray

>> a.class.ancestors=> [NSMutableArray,NSArray,Enumerable,NSObject,Kernel]

>> a << “RubyConf”=> [“RubyConf”]

Page 25: MacRuby to The Max

MACRUBY VS OBJECTIVE-C

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {}

Page 26: MacRuby to The Max

MACRUBY VS OBJECTIVE-C

def locationManager(manager didUpdateToLocation:newLocation

fromLocation:oldLocation)end

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {}

Page 27: MacRuby to The Max

MACRUBY VS OBJECTIVE-C

def locationManager(manager didUpdateToLocation:newLocation

fromLocation:oldLocation)end

>> locationManager(manager didUpdateToLocation:newLocation, fromLocation:oldLocation)

Page 28: MacRuby to The Max

MACRUBY VS OBJECTIVE-C

def locationManager(manager didUpdateToLocation:newLocation

fromLocation:oldLocation)end

>> locationManager(manager :didUpdateToLocation => newLocation, :fromLocation => oldLocation)

Page 29: MacRuby to The Max

MACRUBY VS OBJECTIVE-C

NSString *myString;@property(nonatomic,retain) IBOutlet NSString *myString;

attr_accessor :my_string

Interface Builder Outlets

=

# Interface (header file)

Page 30: MacRuby to The Max

MACRUBY VS OBJECTIVE-C

-(IBAction) myAction:(id)sender;

def my_action(sender)...

end

Interface Builder Actions

=

# Interface

-(IBAction) myAction:(id)sender { ... }# Implementation

Page 31: MacRuby to The Max

WAIT, WHAT ABOUT MACRUBY FOR IPHONE/IPAD?

Page 32: MacRuby to The Max

MACRUBY

Xcode

Page 33: MacRuby to The Max

MACRUBY

Interface Builder

Page 34: MacRuby to The Max

MACRUBY

Instruments

Page 35: MacRuby to The Max

MACRUBY

Let’s build something ...

Page 36: MacRuby to The Max

TDD IN MACRUBY

• Good supports for tests

• Any Ruby testing framework instantly becomes an Objective-C testing framework

• Test::Unit

• RSpec

• Cucumber

• etc...

Page 37: MacRuby to The Max

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

Page 38: MacRuby to The Max

MACRUBY VS HOTCOCOA

win = NSWindow.alloc.initWithContentRect([10,20,300,300],:styleMask => (NSTitleWindowMask | NSCloseableWindowMask | NSMiniatureizableWindowMask | NSResizeableWindowMask)

win = window :frame => [10,20,300,300]

=

Page 39: MacRuby to The Max

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

Page 40: MacRuby to The Max

MACRUBY

Very exciting future

Page 41: MacRuby to The Max

MACRUBY

It’s Ruby for Cocoa Apps!