Macruby - RubyConf Presentation 2010

88
MACRUBY Apple’s Ruby Matt Aimonetti - RubyConf X - Nov 2010 Friday, November 12, 2010

description

Presentation of what's new with MacRuby since RubyConf 2009

Transcript of Macruby - RubyConf Presentation 2010

Page 1: Macruby - RubyConf Presentation 2010

MACRUBYA p p l e ’ s R u b y

M a t t A i m o n e t t i - R u b y C o n f X - N o v 2 0 1 0

Friday, November 12, 2010

Page 2: Macruby - RubyConf Presentation 2010

ABOUT ME

Friday, November 12, 2010

Page 3: Macruby - RubyConf Presentation 2010

Friday, November 12, 2010

Page 4: Macruby - RubyConf Presentation 2010

Hacker

Friday, November 12, 2010

Page 5: Macruby - RubyConf Presentation 2010

MacRuby

Friday, November 12, 2010

Page 6: Macruby - RubyConf Presentation 2010

http://macruby.labs.oreilly.com/

Friday, November 12, 2010

Page 7: Macruby - RubyConf Presentation 2010

MACRUBYA p p l e ’ s R u b y

Friday, November 12, 2010

Page 10: Macruby - RubyConf Presentation 2010

Mac

Rub

y

LLVM/Roxor

Parser Stdlib

AOT JIT

Obj-C Runtime

libobjc

libauto

Builtin Classes

CoreFoundation

YARV

Parser

GC

Runtime

Stdlib

Builtin classes

Rub

y 1.

9

Friday, November 12, 2010

Page 11: Macruby - RubyConf Presentation 2010

MacRuby

LLVM

Friday, November 12, 2010

Page 12: Macruby - RubyConf Presentation 2010

JIT Compilation

MacRuby

Friday, November 12, 2010

Page 13: Macruby - RubyConf Presentation 2010

JIT CompilationMacRuby

Ruby AST

LLVM IR

MachineCode

CPU

Transforms the AST into LLVM intermediate representation

... optimizes it

... compiles it to machine code

... and executes it!

Very good performance for algorithmic operations

Friday, November 12, 2010

Page 14: Macruby - RubyConf Presentation 2010

AOT Compilation

MacRuby

Friday, November 12, 2010

Page 15: Macruby - RubyConf Presentation 2010

AOT CompilationMacRuby

Ruby AST

LLVM IR

MachineCode

Mach-OObject File

CLI tool or Xcode

... applies more optimizations

... and saves the machine code to disk

... faster bootstrap time

Can be used to obfuscate the source code

Friday, November 12, 2010

Page 16: Macruby - RubyConf Presentation 2010

NO GILNo Global Interpreter Lock

Friday, November 12, 2010

Page 17: Macruby - RubyConf Presentation 2010

GC Thread Thread

VM VM VM

Core

Friday, November 12, 2010

Page 18: Macruby - RubyConf Presentation 2010

MULTI-THREADED GENERATIONAL

GCFriday, November 12, 2010

Page 19: Macruby - RubyConf Presentation 2010

NATIVE ACCESS TO COCOA

Friday, November 12, 2010

Page 20: Macruby - RubyConf Presentation 2010

WHEN .TO USE

MACRUBY?

NOT

Friday, November 12, 2010

Page 21: Macruby - RubyConf Presentation 2010

YOU DON’T HAVE A MAC

Friday, November 12, 2010

Page 22: Macruby - RubyConf Presentation 2010

LINUX HOSTING

Friday, November 12, 2010

Page 23: Macruby - RubyConf Presentation 2010

YOU DON’T WANT TO LEARN

Friday, November 12, 2010

Page 24: Macruby - RubyConf Presentation 2010

WHAT’S NEW?

Friday, November 12, 2010

Page 25: Macruby - RubyConf Presentation 2010

COCOA DEVIS STABLE

Friday, November 12, 2010

Page 26: Macruby - RubyConf Presentation 2010

DNA Sequencing

by Hampton Catlin

Friday, November 12, 2010

Page 27: Macruby - RubyConf Presentation 2010

AOT COMPILATION

Friday, November 12, 2010

Page 28: Macruby - RubyConf Presentation 2010

MacRuby: De!nitive Guide

Hello World Example

Friday, November 12, 2010

Page 29: Macruby - RubyConf Presentation 2010

framework 'AppKit'

class AppDelegate def applicationDidFinishLaunching(notification) voice_type = "com.apple.speech.synthesis.voice.GoodNews" @voice = NSSpeechSynthesizer.alloc.initWithVoice(voice_type) end def windowWillClose(notification) puts "Bye!" exit end def say_hello(sender) @voice.startSpeakingString("Hello World!") puts "Hello World!" endend

app = NSApplication.sharedApplicationapp.delegate = AppDelegate.new

window = NSWindow.alloc.initWithContentRect([200, 300, 300, 100], styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask, backing:NSBackingStoreBuffered, defer:false)window.title = 'MacRuby: The Definitive Guide'window.level = 3window.delegate = app.delegate

button = NSButton.alloc.initWithFrame([80, 10, 120, 80])button.bezelStyle = 4button.title = 'Hello World!'button.target = app.delegatebutton.action = 'say_hello:'

window.contentView.addSubview(button)

window.displaywindow.orderFrontRegardlessapp.run

hello_world.rb

Friday, November 12, 2010

Page 30: Macruby - RubyConf Presentation 2010

$ macrubyc hello_world.rb -o demo

$ file demo demo: Mach-O 64-bit executable x86_64$ ./demo

Compile to machine code

Friday, November 12, 2010

Page 31: Macruby - RubyConf Presentation 2010

w00t!!

Friday, November 12, 2010

Page 32: Macruby - RubyConf Presentation 2010

Create compiled/

dynamic libra

ries

$ macrubyc file1.rb file2.rb -o shared.dylib --dylib

$ macrubyc other_project_source.rb shared.dylib -o example

$ ./example

Friday, November 12, 2010

Page 33: Macruby - RubyConf Presentation 2010

DEBUGGER

Friday, November 12, 2010

Page 34: Macruby - RubyConf Presentation 2010

characters = %w{Adama Apollo Baltar Roslin StarBuck Six} def cylon?(character) false # buggy detectorend characters.each do |character| if cylon?(character) puts "#{character} is a Cylon!" else puts "#{character} is not a cylon." endend

Friday, November 12, 2010

Page 35: Macruby - RubyConf Presentation 2010

$ macruby cylon_detector.rb Adama is not a cylon.Apollo is not a cylon.Baltar is not a cylon.Roslin is not a cylon.StarBuck is not a cylon.Six is not a cylon.

Buggy code :(

Friday, November 12, 2010

Page 36: Macruby - RubyConf Presentation 2010

$ macrubyd cylon_detector.rbStarting program.1!characters = %w{Adama Apollo Baltar Roslin StarBuck Six}cylon_detector.rb:1>

Debug the detector live

Friday, November 12, 2010

Page 37: Macruby - RubyConf Presentation 2010

cylon_detector.rb:1> b cylon_detector.rb:8 if character == 'Six'Added breakpoint 1.

Added a breakpoint on

a

speci!c !le/line based on

a condition

Friday, November 12, 2010

Page 38: Macruby - RubyConf Presentation 2010

cylon_detector.rb:1> cAdama is not a cylon.Apollo is not a cylon.Baltar is not a cylon.Roslin is not a cylon.StarBuck is not a cylon.8! if cylon?(character)cylon_detector.rb:8>

Breaks on condition

Friday, November 12, 2010

Page 39: Macruby - RubyConf Presentation 2010

cylon_detector.rb:8> p character=> "Six"cylon_detector.rb:8> p cylon?(character)=> false

Bug con!rmed :(

Friday, November 12, 2010

Page 40: Macruby - RubyConf Presentation 2010

cylon_detector.rb:8> p def cylon?(character) character == 'Six' end

ZOMG live bug !xing!

Friday, November 12, 2010

Page 41: Macruby - RubyConf Presentation 2010

cylon_detector.rb:8> cSix is a Cylon!Program exited.

w00t!!

Friday, November 12, 2010

Page 42: Macruby - RubyConf Presentation 2010

GCD API

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/Friday, November 12, 2010

Page 43: Macruby - RubyConf Presentation 2010

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

Threads can be complicated to program :(

Friday, November 12, 2010

Page 44: Macruby - RubyConf Presentation 2010

Global Interpreter Lock

VM

Friday, November 12, 2010

Page 45: Macruby - RubyConf Presentation 2010

Slow :(

Friday, November 12, 2010

Page 46: Macruby - RubyConf Presentation 2010

Full concurrency via

multiple processes :(

Friday, November 12, 2010

Page 47: Macruby - RubyConf Presentation 2010

Safe data :)

Friday, November 12, 2010

Page 48: Macruby - RubyConf Presentation 2010

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

MacRuby doesn’t have

a global VM lock

Friday, November 12, 2010

Page 49: Macruby - RubyConf Presentation 2010

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

MacRuby

VM

Core

VMVM

Friday, November 12, 2010

Page 50: Macruby - RubyConf Presentation 2010

Good but not good

enough, threads are

still

a pain in the butt!

Friday, November 12, 2010

Page 51: Macruby - RubyConf Presentation 2010

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

libdispatch

Friday, November 12, 2010

Page 52: Macruby - RubyConf Presentation 2010

Let GCD !gure out how

many threads to use!

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

require 'dispatch'job = Dispatch::Job.new { slow_operation }job.value # => “wait for the result”

Friday, November 12, 2010

Page 53: Macruby - RubyConf Presentation 2010

Asynchronous API

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

require 'dispatch'job = Dispatch::Job.new { slow_operation }job.value do |v| "asynchronous dispatch when done!"end

Friday, November 12, 2010

Page 54: Macruby - RubyConf Presentation 2010

Elegant wrapper API

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

require 'dispatch'job = Dispatch::Job.new job.add { slow_operation(a) }job.add { slow_operation(b) }job.add { slow_operation(c) }job.joinjob.values # =>[result_a, result_b, result_c]

Friday, November 12, 2010

Page 55: Macruby - RubyConf Presentation 2010

Dispatch Enumerable

Parallel Iterations

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

require 'dispatch'geeks.p_each do |user| print find_pair(user)end

Friday, November 12, 2010

Page 56: Macruby - RubyConf Presentation 2010

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

Remember: MacRuby

doesn’t have a globa

l

VM lock!

Friday, November 12, 2010

Page 57: Macruby - RubyConf Presentation 2010

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

Serial queues:

lock free synchro

nization

Friday, November 12, 2010

Page 58: Macruby - RubyConf Presentation 2010

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

require 'dispatch'job = Dispatch::Job.new @scores = job.synchronize Hash.new[user1, user2, user3].each do |user| job.add \ { @scores[user.name] = calculate(user) }endjob.join@scores[user1.name] # => 1042@scores[user2.name] # => 673@scores[user3.name] # => 845

Friday, November 12, 2010

Page 59: Macruby - RubyConf Presentation 2010

https://github.com/MacRuby/MacRuby/tree/trunk/lib/dispatch/

require 'dispatch'job = Dispatch::Job.new @scores = job.synchronize Hash.new[user1, user2, user3].each do |user| job.add \ { @scores[user.name] = calculate(user) }endjob.join@scores[user1.name] # => 1042@scores[user2.name] # => 673@scores[user3.name] # => 845

safe proxy objec

t

Friday, November 12, 2010

Page 60: Macruby - RubyConf Presentation 2010

CONTROLTOWER

http://github.com/MacRuby/ControlTowerFriday, November 12, 2010

Page 61: Macruby - RubyConf Presentation 2010

http://github.com/MacRuby/ControlTower

Web server for

Rack apps

Friday, November 12, 2010

Page 62: Macruby - RubyConf Presentation 2010

NEW DISPATCHER

Friday, November 12, 2010

Page 63: Macruby - RubyConf Presentation 2010

ONIGURUMA TO ICU

Friday, November 12, 2010

Page 64: Macruby - RubyConf Presentation 2010

MORE SOLID FOUNDATIONS

Friday, November 12, 2010

Page 65: Macruby - RubyConf Presentation 2010

SUPPORT FOR C BLOCKS

Friday, November 12, 2010

Page 66: Macruby - RubyConf Presentation 2010

SANDBOX

Friday, November 12, 2010

Page 67: Macruby - RubyConf Presentation 2010

$ macirb --simple-prompt>> require 'open-uri'=> true>> begin>> Sandbox.no_internet.apply!>> open('http://www.macruby.org')>> rescue SystemCallError => exception>> puts exception>> endOperation not permitted - connect(2)=> nil

Friday, November 12, 2010

Page 68: Macruby - RubyConf Presentation 2010

•Sandbox.pure_computation •Sandbox.no_internet•Sandbox.no_network•Sandbox.temporary_writes•Sandbox.no_writes

5 default pro!les

Friday, November 12, 2010

Page 69: Macruby - RubyConf Presentation 2010

APP STORE

Friday, November 12, 2010

Page 70: Macruby - RubyConf Presentation 2010

Great exposure!

Friday, November 12, 2010

Page 71: Macruby - RubyConf Presentation 2010

Better user

experience

Friday, November 12, 2010

Page 72: Macruby - RubyConf Presentation 2010

Reinvent desktop apps

Friday, November 12, 2010

Page 73: Macruby - RubyConf Presentation 2010

/Developer/Examples/Ruby/MacRuby/

What can you do

with MacRuby?!

Friday, November 12, 2010

Page 74: Macruby - RubyConf Presentation 2010

Speech

Recognizer

Friday, November 12, 2010

Page 75: Macruby - RubyConf Presentation 2010

Friday, November 12, 2010

Page 76: Macruby - RubyConf Presentation 2010

CoreLocation

Friday, November 12, 2010

Page 77: Macruby - RubyConf Presentation 2010

Friday, November 12, 2010

Page 78: Macruby - RubyConf Presentation 2010

Friday, November 12, 2010

Page 79: Macruby - RubyConf Presentation 2010

Address

Book.app

Friday, November 12, 2010

Page 80: Macruby - RubyConf Presentation 2010

Friday, November 12, 2010

Page 81: Macruby - RubyConf Presentation 2010

String Tokeni

zer

Friday, November 12, 2010

Page 82: Macruby - RubyConf Presentation 2010

framework 'Foundation'class String def language CFStringTokenizerCopyBestStringLanguage(self, CFRangeMake(0, self.size)) endend

Call a C function

directly

Friday, November 12, 2010

Page 83: Macruby - RubyConf Presentation 2010

framework 'Foundation'class String def language CFStringTokenizerCopyBestStringLanguage(self, CFRangeMake(0, self.size)) endend

["Bonne année!", "Happy new year!", "¡Feliz año nuevo!", "Felice anno nuovo!", "أعياد سعيدة", "明けましておめでとうございます。"].each do |msg| puts "#{msg} (#{msg.language})"end

Friday, November 12, 2010

Page 84: Macruby - RubyConf Presentation 2010

Bonne année! (fr)Happy new year! (en)¡Feliz año nuevo! (es)Felice anno nuovo! (it)(ar) أعياد سعيدة明けましておめでとうございます。 (ja)

Friday, November 12, 2010

Page 85: Macruby - RubyConf Presentation 2010

BluetoothHardw

are

WebKit

JS Bridge

Friday, November 12, 2010

Page 86: Macruby - RubyConf Presentation 2010

Friday, November 12, 2010

Page 88: Macruby - RubyConf Presentation 2010

@merbist

http://merbist.com

http://macruby.labs.oreilly.com

http://github.com/mattettiFriday, November 12, 2010