Macruby& Hotcocoa presentation by Rich Kilmer

23
MacRuby + HotCocoa by rICH kILMER Golden Gate Ruby Conf

Transcript of Macruby& Hotcocoa presentation by Rich Kilmer

Page 1: Macruby& Hotcocoa presentation by Rich Kilmer

MacRuby + HotCocoa

by rICH kILMERGolden Gate Ruby Conf

Page 2: Macruby& Hotcocoa presentation by Rich Kilmer

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

Page 3: Macruby& Hotcocoa presentation by Rich Kilmer

Apple’s Goals:

Make Mac OS X the best platform for Ruby developers

Page 4: Macruby& Hotcocoa presentation by Rich Kilmer

Apple’s Goals:

Make Ruby a first class Cocoa programming language

on Mac OS X

the best

Page 5: Macruby& Hotcocoa presentation by Rich Kilmer

User Experience

Application Frameworks

Graphics and Media

Darwin

Mac OS X Stack

Page 6: Macruby& Hotcocoa presentation by Rich Kilmer

User Experience

Application Frameworks

Graphics and Media

Darwin

Mac OS X Stack - Languages

Objective-C

C

Page 7: Macruby& Hotcocoa presentation by Rich Kilmer

Bridging Ruby & Objective-C

RubyCocoaby

by FUJIMOTO Hisakuni (2001)

Bundled with Mac OS X 10.5 (stable)

Page 8: Macruby& Hotcocoa presentation by Rich Kilmer

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

Page 9: Macruby& Hotcocoa presentation by Rich Kilmer

Problems with RubyCocoa:

It’s a bridge

Messaging syntax is different

Ruby uses green threads

Two runtimes, two GCs

Page 10: Macruby& Hotcocoa presentation by Rich Kilmer

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

Page 11: Macruby& Hotcocoa presentation by Rich Kilmer

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

Page 12: Macruby& Hotcocoa presentation by Rich Kilmer

Enter HotCocoa

HotCocoa is an idiomaticRuby API that simplifies the

configuration and wiring together of ObjC/Cocoa classes

Page 13: Macruby& Hotcocoa presentation by Rich Kilmer

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

Page 14: Macruby& Hotcocoa presentation by Rich Kilmer

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

Page 15: Macruby& Hotcocoa presentation by Rich Kilmer

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

Page 16: Macruby& Hotcocoa presentation by Rich Kilmer

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

Page 17: Macruby& Hotcocoa presentation by Rich Kilmer

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

Page 18: Macruby& Hotcocoa presentation by Rich Kilmer

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

Page 19: Macruby& Hotcocoa presentation by Rich Kilmer

HotCocoa Library hotcocoa Command

Page 20: Macruby& Hotcocoa presentation by Rich Kilmer

hotcocoa <app> Rakefile

config

build.yml

lib

menu.rb

lib

application.rb

resources

HotCocoa.icns

Page 21: Macruby& Hotcocoa presentation by Rich Kilmer

Demo

Page 22: Macruby& Hotcocoa presentation by Rich Kilmer

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

Page 23: Macruby& Hotcocoa presentation by Rich Kilmer

by rICH kILMERGolden Gate Ruby Conf

www.macruby.org@macruby