RubyMotion:
Under the Hood
Inspired by Click and Clack

Monday, October 14, 2013
A brief introduction

Monday, October 14, 2013
About Me

Name: Joshua Ballanco
Monday, October 14, 2013
About Me

Employer: Burnside Digital
Monday, October 14, 2013
About Me

Location: Ankara, Turkey
Monday, October 14, 2013
A slightly longer
introduction

Monday, October 14, 2013
Before...

Monday, October 14, 2013
About Me

Employer: Patch
Monday, October 14, 2013
About Me

Location: New York City
Monday, October 14, 2013
Before that...

Monday, October 14, 2013
About Me

Employer: Apple
Monday, October 14, 2013
About Me

Location: Cupertino
Monday, October 14, 2013
Before that...

Monday, October 14, 2013
About Me

School: University of Miami
Monday, October 14, 2013
About Me

Location: Miami
Monday, October 14, 2013
A bit of history

Monday, October 14, 2013
In graduate school

Monday, October 14, 2013
In graduate school

Monday, October 14, 2013
At Apple... Retail

Monday, October 14, 2013
At Apple

Laurent Sansonetti

Monday, October 14, 2013
After Apple...

Monday, October 14, 2013
Episode VI - The Return
of The RubyMotion

Monday, October 14, 2013
What is RubyMotion?
• Use Ruby to build apps for iOS and OS X
• Native apps
• Interface directly with Obj-C libraries
• CLI-based build system

Monday, October 14, 2013
What is RubyMotion?
• RubyMotion: http://www.rubymotion.com/
• MotionCasts: http://motioncasts.tv/
• RubyMotion Wrappers:
!

http://rubymotion-wrappers.com/

• ...and lot’s more

Monday, October 14, 2013
What is MacRuby?
• Intended to be the implementation of Ruby
2.0 for OS X

• Target RubySpec compliance
• JIT or AOT Compiled
• Uses libauto for Garbage Collection

Monday, October 14, 2013
What is MacRuby?
Ruby Syntax

Parser

Compiler

VM (sans GVL)

LLVM (with JIT)

Objective-C Runtime

Monday, October 14, 2013
Running a “something.rb” file
Ruby Syntax

Parser

Compiler

VM (sans GVL)

LLVM (with JIT)

Objective-C Runtime

Monday, October 14, 2013
Running a “something.rb” file
Ruby Syntax

Parser

Compiler

VM (sans GVL)

LLVM (with JIT)

Objective-C Runtime

Monday, October 14, 2013
Running a “something.rb” file
Ruby Syntax

Parser

Compiler

VM (sans GVL)

LLVM (with JIT)

Objective-C Runtime

Monday, October 14, 2013
AOT compiling “something.rb”
Ruby Syntax

Parser

Compiler

VM (sans GVL)

Objective-C Runtime

Monday, October 14, 2013

LLVM

something.o
AOT compiling “something.rb”
Ruby Syntax

Parser

Compiler

VM (sans GVL)

Objective-C Runtime

Monday, October 14, 2013

LLVM

something.o
Running an AOT compiled
“something.rb”
Parser

Compiler

VM (sans GVL)

Objective-C Runtime

Monday, October 14, 2013

something.o
Running an AOT compiled
“something.rb”
Parser

Compiler

VM (sans GVL)

Objective-C Runtime

Monday, October 14, 2013

something.o
Running an AOT compiled
“something.rb”
Parser

Compiler
LLVM
VM (sans GVL)

Objective-C Runtime

Monday, October 14, 2013

something.o
Running an AOT compiled
“something.rb”
Parser

Compiler
LLVM
VM (sans GVL)

Objective-C Runtime

Monday, October 14, 2013

something.o
Running an AOT compiled
“something.rb”
Parser

Compiler
LLVM
VM (sans GVL)

Objective-C Runtime

Monday, October 14, 2013

something.o
What is RubyMotion?
• Descendent of MacRuby
• “Ruby, the Good Parts”
• Static Compiled
• Retain/release reference counting

Monday, October 14, 2013
Static compiling “something.rb”
Ruby Syntax

Parser

Compiler

VM (sans GVL)

Objective-C Runtime

Monday, October 14, 2013

LLVM

something.o
Static compiling “something.rb”
Ruby Syntax

Parser

Compiler

VM (sans GVL)

Objective-C Runtime

Monday, October 14, 2013

LLVM

something.o
Running a static compiled
“something.rb”
VM (sans GVL)

Objective-C Runtime

Monday, October 14, 2013

something.o
Running a static compiled
“something.rb”
VM (sans GVL)

Objective-C Runtime

Monday, October 14, 2013

something.o
Why must we statically
compile?
• On OS X
• Compile writes code to a memory
page

• Runtime runs the code from that
memory page

Monday, October 14, 2013
Why must we statically
compile?
•

On iOS
Memory pages must be writable or
executable, NOT BOTH!

•
•
•

Monday, October 14, 2013

•

OS prohibits runtime compilation
Apple prohibits interpreting arbitrary scripts
...but you wouldn’t want an interpreter
anyway
Garbage Collection

Monday, October 14, 2013
What happened to the
Garbage Collector?
It required extra threads, so we
had to kill it...

Monday, October 14, 2013
So RubyMotion Uses
ARC?
Yes...
Uh...no
...sorta?

Monday, October 14, 2013
ARC vs “ARC”
• Objective-C’s ARC modifies your code
before compilation

• RubyMotion VM knows when retain and/or
release should be called...your code is not
touched

Monday, October 14, 2013
ARC vs “ARC”
Isn’t the distinction
rather academic?
Probably...

Monday, October 14, 2013
“ARC” Caveats
• Collection happens when the autorelease
pool drains

• Need to be careful with tight loops that
generate many objects

• Use “autorelease do...end”

• Detects almost all cycles
• Use WeakRefs if cycles become
problematic

Monday, October 14, 2013
Debugging RubyMotion
• Remember, RubyMotion objects are
Objective-C objects...

• All the usual tricks are valid!

Monday, October 14, 2013
Let’s Play!

Monday, October 14, 2013
The Victim
app/app_delegate.rb

Monday, October 14, 2013
The Victim
app/app_delegate.rb

Monday, October 14, 2013
REPL Magic

Monday, October 14, 2013
REPL Magic

Monday, October 14, 2013
REPL Magic

Monday, October 14, 2013
REPL Magic

Monday, October 14, 2013
That’s cool...but can
you do it in a
debugger???

Monday, October 14, 2013
Debugger Wizardry

Monday, October 14, 2013
Debugger Wizardry

Monday, October 14, 2013
Debugger Wizardry

Monday, October 14, 2013
Debugger Wizardry

Monday, October 14, 2013
That’s CRAZY!
I know...but it’s fun!

Monday, October 14, 2013
Debugging RubyMotion
• Helper methods “pro” and “pri”
• http://www.rubymotion.com/developercenter/articles/debugging/

• Watch for more/better tooling to come...

Monday, October 14, 2013
Questions?
Joshua Ballanco
@manhattanmetric
https://github.com/jballanc

Monday, October 14, 2013

RubyMotion: Under the Hood