developing cocoa applications with macruby

Post on 15-May-2015

3.585 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Brendan G. Lim@brendanlim

brendan@intridea.com

Developing CocoaApplications With

MacRuby

Saturday, February 5, 2011

• Objective-C & Cocoa

• RubyCocoa

• MacRuby

• Live Coding

• HotCocoa

Outline

Saturday, February 5, 2011

• 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

• 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

Why make desktop apps?

Saturday, February 5, 2011

Different Paradigm

Saturday, February 5, 2011

Mac App Store

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

Saturday, February 5, 2011

Why Ruby insteadof Objective-C?

Saturday, February 5, 2011

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

object.method(param)

[object method:param];

=

Ruby vs Objective-C

Saturday, February 5, 2011

array = []

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

=

Ruby vs Objective-C

Saturday, February 5, 2011

“ string”.strip

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

=

Ruby vs Objective-C

Saturday, February 5, 2011

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

+

Saturday, February 5, 2011

• 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

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

So, Why Not RubyCocoa?

(besides it looking gross)

Saturday, February 5, 2011

• 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

+

Saturday, February 5, 2011

• 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

Object

String

Number

Array

Hash

NSObject

NSMutableString

NSNumber

NSMutableArray

NSMutableDictionary

MacRuby

Saturday, February 5, 2011

Objects

Classes

Methods

Objective-C

Objective-C

Objective-C

MacRuby

Saturday, February 5, 2011

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

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

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

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

MacRuby

Saturday, February 5, 2011

>> a = []=> []

>> a.class=> Array

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

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

MacRuby

Saturday, February 5, 2011

MacRuby vs Objective-C-(id) tableView:(NSTableView *)tableView objectValueForColumn:(NSTableColumn *)column row:(int)rowIndex { .. }

Saturday, February 5, 2011

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

Interface Builder Outlets & Actions

MacRuby vs Objective-C

!

Saturday, February 5, 2011

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

attr_accessor :myString

Interface Builder Outlets

=

# Interface

MacRuby vs Objective-C

Saturday, February 5, 2011

def myAction(sender)...

end

Interface Builder Actions

=

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

MacRuby vs Objective-C

Saturday, February 5, 2011

MacRuby - Gem Support

• sudo macgem install awesome_gem

• Not all gems supported right now

Saturday, February 5, 2011

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

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

• Test::Unit

• RSpec

• etc...

Testing MacRuby

Saturday, February 5, 2011

What tools will webe using?

Saturday, February 5, 2011

Xcode

Saturday, February 5, 2011

Interface Builder

Saturday, February 5, 2011

Instruments

Saturday, February 5, 2011

Let’s build a MacRuby app

Saturday, February 5, 2011

• 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

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

HotCocoa

sudo macgem install hotcocoa

hotcocoa NAME_OF_APP

Saturday, February 5, 2011

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

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

top related