developing cocoa applications with macruby

43
Brendan G. Lim @brendanlim [email protected] Developing Cocoa Applications With MacRuby Saturday, February 5, 2011

Upload: brendan-lim

Post on 15-May-2015

3.585 views

Category:

Technology


1 download

DESCRIPTION

MacRuby presentation at MagicRuby 2011 in Orlando, Florida on February 5, 2011.

TRANSCRIPT

Page 1: Developing Cocoa Applications with macRuby

Brendan G. Lim@brendanlim

[email protected]

Developing CocoaApplications With

MacRuby

Saturday, February 5, 2011

Page 2: Developing Cocoa Applications with macRuby

• Objective-C & Cocoa

• RubyCocoa

• MacRuby

• Live Coding

• HotCocoa

Outline

Saturday, February 5, 2011

Page 3: Developing Cocoa Applications with macRuby

• 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

• 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

Why make desktop apps?

Saturday, February 5, 2011

Page 6: Developing Cocoa Applications with macRuby

Different Paradigm

Saturday, February 5, 2011

Page 7: Developing Cocoa Applications with macRuby

Mac App Store

1. Build MacRuby application2. Submit to App Store4. Profit

Saturday, February 5, 2011

Page 8: Developing Cocoa Applications with macRuby

Why Ruby insteadof Objective-C?

Saturday, February 5, 2011

Page 9: Developing Cocoa Applications with macRuby

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

object.method(param)

[object method:param];

=

Ruby vs Objective-C

Saturday, February 5, 2011

Page 11: Developing Cocoa Applications with macRuby

array = []

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

=

Ruby vs Objective-C

Saturday, February 5, 2011

Page 12: Developing Cocoa Applications with macRuby

“ string”.strip

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

=

Ruby vs Objective-C

Saturday, February 5, 2011

Page 13: Developing Cocoa Applications with macRuby

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

+

Saturday, February 5, 2011

Page 15: Developing Cocoa Applications with macRuby

• 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

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

So, Why Not RubyCocoa?

(besides it looking gross)

Saturday, February 5, 2011

Page 18: Developing Cocoa Applications with macRuby

• 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

+

Saturday, February 5, 2011

Page 20: Developing Cocoa Applications with 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

MacRuby

Saturday, February 5, 2011

Page 21: Developing Cocoa Applications with macRuby

Object

String

Number

Array

Hash

NSObject

NSMutableString

NSNumber

NSMutableArray

NSMutableDictionary

MacRuby

Saturday, February 5, 2011

Page 22: Developing Cocoa Applications with macRuby

Objects

Classes

Methods

Objective-C

Objective-C

Objective-C

MacRuby

Saturday, February 5, 2011

Page 23: Developing Cocoa Applications with macRuby

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

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

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

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

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

Interface Builder Outlets & Actions

MacRuby vs Objective-C

!

Saturday, February 5, 2011

Page 29: Developing Cocoa Applications with macRuby

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

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

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

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

• 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

What tools will webe using?

Saturday, February 5, 2011

Page 35: Developing Cocoa Applications with macRuby

Xcode

Saturday, February 5, 2011

Page 36: Developing Cocoa Applications with macRuby

Interface Builder

Saturday, February 5, 2011

Page 37: Developing Cocoa Applications with macRuby

Instruments

Saturday, February 5, 2011

Page 38: Developing Cocoa Applications with macRuby

Let’s build a MacRuby app

Saturday, February 5, 2011

Page 39: Developing Cocoa Applications with macRuby

• 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

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

HotCocoa

sudo macgem install hotcocoa

hotcocoa NAME_OF_APP

Saturday, February 5, 2011

Page 42: Developing Cocoa Applications with macRuby

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

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