Transcript

MacRuby + HotCocoa

by rICH kILMERGolden Gate Ruby Conf

A History of Apple and Ruby

OS X 10.2Ruby 1.6.7

2002 2003 2004 2005 2006 2007 2008 2009

OS X 10.4Ruby 1.8.2

OS X 10.5Ruby 1.8.6RubyGemsRubyCocoa

Rails

OS X 10.xRuby 1.8.7RubyGemsRubyCocoa

Rails 2.2

MacRuby

Apple’s Goals:

Make Mac OS X the best platform for Ruby developers

Apple’s Goals:

Make Ruby a first class Cocoa programming language

on Mac OS X

the best

User Experience

Application Frameworks

Graphics and Media

Darwin

Mac OS X Stack

User Experience

Application Frameworks

Graphics and Media

Darwin

Mac OS X Stack - Languages

Objective-C

C

Bridging Ruby & Objective-C

RubyCocoaby

by FUJIMOTO Hisakuni (2001)

Bundled with Mac OS X 10.5 (stable)

require 'osx/cocoa'; include OSX

app = NSApplication.sharedApplication

win = NSWindow.alloc.initWithContentRect_styleMask_backing_defer( [0, 0, 200, 60], NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask, NSBackingStoreBuffered, false) win.title = 'Hello World'

button = NSButton.alloc.initWithFrame(NSZeroRect) win.contentView.addSubview(button) button.bezelStyle = NSRoundedBezelStyle button.title = 'Hello!' button.sizeToFitbutton.frameOrigin = NSMakePoint((win.contentView.frameSize.width / 2.0) - (button.frameSize.width / 2.0), (win.contentView.frameSize.height / 2.0) - (button.frameSize.height / 2.0))

button_controller = Object.new def button_controller.sayHello(sender) puts "Hello World!" end button.target = button_controller button.action = 'sayHello:'

win.display win.orderFrontRegardless

app.run

RubyCocoa Hello World

Problems with RubyCocoa:

It’s a bridge

Messaging syntax is different

Ruby uses green threads

Two runtimes, two GCs

Objective-C 2.0

MacRuby 0.4

Ruby 1.9Core

FoundationGarbage Collector Runtime YARV Parser Standard

Library

Enter MacRuby

Garbage Collector Built-ins

Every Ruby class is an Objective-C classEvery Ruby object is an Objective-C object

Every Ruby method is an Objective-C method

framework ‘Cocoa’

app = NSApplication.sharedApplication

win = NSWindow.alloc.initWithContentRect([0, 0, 200, 60], styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask, backing:NSBackingStoreBuffered, defer:false)

win.title = 'Hello World'

button = NSButton.alloc.initWithFrame(NSZeroRect) win.contentView.addSubview(button) button.bezelStyle = NSRoundedBezelStyle button.title = 'Hello!' button.sizeToFitbutton.frameOrigin = NSMakePoint((win.contentView.frameSize.width / 2.0) - (button.frameSize.width / 2.0), (win.contentView.frameSize.height / 2.0) - (button.frameSize.height / 2.0))

button_controller = Object.new def button_controller.sayHello(sender) puts "Hello World!" end button.target = button_controller button.action = 'sayHello:'

win.display win.orderFrontRegardless

app.run

MacRuby Hello World

Enter HotCocoa

HotCocoa is an idiomaticRuby API that simplifies the

configuration and wiring together of ObjC/Cocoa classes

MacRuby Hello Worldframework ‘Cocoa’

app = NSApplication.sharedApplication

win = NSWindow.alloc.initWithContentRect([0, 0, 200, 60], styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask, backing:NSBackingStoreBuffered, defer:false)

win.title = 'Hello World'

button = NSButton.alloc.initWithFrame(NSZeroRect) win.contentView.addSubview(button) button.bezelStyle = NSRoundedBezelStyle button.title = 'Hello!' button.sizeToFitbutton.frameOrigin = NSMakePoint((win.contentView.frameSize.width / 2.0) - (button.frameSize.width / 2.0), (win.contentView.frameSize.height / 2.0) - (button.frameSize.height / 2.0))

button_controller = Object.new def button_controller.sayHello(sender) puts "Hello World!" end button.target = button_controller button.action = 'sayHello:'

win.display win.orderFrontRegardless

app.run

require ‘hotcocoa’; include HotCocoa

app = NSApplication.sharedApplication

win = NSWindow.alloc.initWithContentRect([0, 0, 200, 60], styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask, backing:NSBackingStoreBuffered, defer:false)

win.title = 'Hello World'

button = NSButton.alloc.initWithFrame(NSZeroRect) win.contentView.addSubview(button) button.bezelStyle = NSRoundedBezelStyle button.title = 'Hello!' button.sizeToFitbutton.frameOrigin = NSMakePoint((win.contentView.frameSize.width / 2.0) - (button.frameSize.width / 2.0), (win.contentView.frameSize.height / 2.0) - (button.frameSize.height / 2.0))

button_controller = Object.new def button_controller.sayHello(sender) puts "Hello World!" end button.target = button_controller button.action = 'sayHello:'

win.display win.orderFrontRegardless

app.run

HotCocoa Hello World

HotCocoa Hello Worldrequire ‘hotcocoa’; include HotCocoa

app = NSApplication.sharedApplication

win = window :title => ‘hello world’, :frame => [0, 0, 200, 60]

button = NSButton.alloc.initWithFrame(NSZeroRect) win.contentView.addSubview(button) button.bezelStyle = NSRoundedBezelStyle button.title = 'Hello!' button.sizeToFitbutton.frameOrigin = NSMakePoint((win.contentView.frameSize.width / 2.0) - (button.frameSize.width / 2.0), (win.contentView.frameSize.height / 2.0) - (button.frameSize.height / 2.0))

button_controller = Object.new def button_controller.sayHello(sender) puts "Hello World!" end button.target = button_controller button.action = 'sayHello:'

win.display win.orderFrontRegardless

app.run

HotCocoa Hello Worldrequire ‘hotcocoa’; include HotCocoa

app = NSApplication.sharedApplication

win = window :title => ‘hello world’, :frame => [0, 0, 200, 60]

b = button :title => ‘Hello!’, :layout => {:align => :center}win << b

button_controller = Object.new def button_controller.sayHello(sender) puts "Hello World!" end button.target = button_controller button.action = 'sayHello:'

win.display win.orderFrontRegardless

app.run

HotCocoa Hello Worldrequire ‘hotcocoa’; include HotCocoa

app = NSApplication.sharedApplication

win = window :title => ‘hello world’, :frame => [0, 0, 200, 60]

b = button :title => ‘Hello!’, :layout => {:align => :center}win << b

b.on_action { puts “Hello World!” }

win.display win.orderFrontRegardless

app.run

HotCocoa Hello Worldrequire ‘hotcocoa’; include HotCocoa

application do

win = window :title => ‘hello world’, :frame => [0, 0, 200, 60]

b = button :title => ‘Hello!’, :layout => {:align => :center} win << b

b.on_action { puts “Hello World!” }

end

HotCocoa Library hotcocoa Command

hotcocoa <app> Rakefile

config

build.yml

lib

menu.rb

lib

application.rb

resources

HotCocoa.icns

Demo

Objective-C 2.0

MacRuby 0.5

Ruby 1.9

Core Foundation

Garbage Collector Runtime Parser Standard

Library

MacRuby Experimental

Garbage Collector Built-ins

Numerous optimizations (speed!)No libffi for external calls, new bridgesupport

Passing many RubySpecs already! Will implement fully concurrent threading

YARV

LLVM

JIT

AOT Disk/SocketIO

by rICH kILMERGolden Gate Ruby Conf

www.macruby.org@macruby

top related