macruby
TRANSCRIPT
![Page 1: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/1.jpg)
MacRubyWhat is it?
and why should you care?
Presented by: Joshua Ballanco
![Page 2: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/2.jpg)
A bit about me...
• Using Ruby since 2004 (didn’t discover Rails until 2006)
• Worked for Apple 2006-2010 (Fifth Ave store & HQ in Cupertino)
• Now at Patch (just completed a migration to Ruby 1.9)
• MacRuby Core Team member
![Page 3: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/3.jpg)
Overview• Practical MacRuby
• How is MacRuby different?
• Working with Cocoa
• Compiled Ruby!
• Make your millions on the App store
• Nuts & Bolts
• GC
• Regular Expression Engine
• Hash method syntax
• LLVM & other Hacking
• GCD
![Page 4: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/4.jpg)
What is MacRuby?
![Page 5: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/5.jpg)
What is MacRuby?
![Page 6: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/6.jpg)
What is MacRuby?
![Page 7: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/7.jpg)
What is MacRuby?
![Page 8: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/8.jpg)
What is MacRuby?
• http://www.macruby.org
• http://www.macruby.org/files/
• http://www.macruby.org/files/nightlies/
• Available from RVM (but not recommended)
• Source now located at GitHub!
https://github.com/MacRuby/MacRuby
![Page 9: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/9.jpg)
What is MacRuby?
• Compiling MacRuby:
• Need LLVM (see the README; brew install llvm)
• rake ; sudo rake install
• Bugs – https://www.macruby.org/trac/report
• MacRuby-devel mailing list
• @macruby, #macruby
![Page 10: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/10.jpg)
No Really, What is MacRuby?
![Page 11: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/11.jpg)
No Really, What is MacRuby?
A language with:
• Dynamic typing
• Objects everywhere
• Method calls via sending messages
• Automatic generation of accessors
• Methods can be added to classes at runtime
• Garbage Collection
• Closures
![Page 12: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/12.jpg)
No Really, What is MacRuby?
A language with:
• Dynamic typing
• Objects everywhere
• Method calls via sending messages
• Automatic generation of accessors
• Methods can be added to classes at runtime
• Garbage Collection
• Closures
...called Objective-C
![Page 13: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/13.jpg)
Ruby Syntax
![Page 14: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/14.jpg)
Ruby Syntax
ParserMRI’s parse.y
![Page 15: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/15.jpg)
Ruby Syntax
ParserMRI’s parse.y
CompilerRoxorCompiler <C++>
![Page 16: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/16.jpg)
Ruby Syntax
ParserMRI’s parse.y
CompilerRoxorCompiler <C++>
VM (sans GVL)RoxorVM & RoxorCore <C++>
![Page 17: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/17.jpg)
Ruby Syntax
ParserMRI’s parse.y
CompilerRoxorCompiler <C++>
VM (sans GVL)RoxorVM & RoxorCore <C++>
LLVM (with JIT)
![Page 18: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/18.jpg)
Ruby Syntax
ParserMRI’s parse.y
CompilerRoxorCompiler <C++>
VM (sans GVL)RoxorVM & RoxorCore <C++>
LLVM (with JIT)
![Page 19: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/19.jpg)
Ruby Syntax
ParserMRI’s parse.y
CompilerRoxorCompiler <C++>
VM (sans GVL)RoxorVM & RoxorCore <C++>
LLVM (with JIT)
Objective-C Runtime
![Page 20: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/20.jpg)
Integrating with Cocoa
• Bridge Support (please download preview 3 from the “files” page)
• gen_bridge_metadata reads header files and generates XML descriptions of types and method signatures
• MacRuby uses BS files to construct calls and access constants and types
• framework “Foundation”
![Page 21: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/21.jpg)
Integrating with Cocoa
• String, Array, Hash, are members of the NSString, NSArray, and NSDictionary class clusters
• Caution: NSString, NSArray, and NSDictionary will lie to you!
• Time is implemented as a subclass of NSDate
![Page 22: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/22.jpg)
Demo
![Page 23: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/23.jpg)
A Ruby You Can Compile
• macrubyc
• Advantages:
• Faster startup, faster runtime (in most cases)
• Don’t distribute the source!
• Disadvantages:
• ?
• ...some bugs
![Page 24: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/24.jpg)
A Ruby You Can Compile
• -c → Compile and assemble; good for multistage builds
• -C → Compile, assemble, and link creating an *.rbo
• Can also create stand-alone executables and entire “*.framework”s (experimental)
![Page 25: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/25.jpg)
Demo
![Page 26: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/26.jpg)
I wanna be an App store billionaire...
• Downloads contain Xcode templates (mostly work in Xcode 4)
• Integration with Interface Builder
• macruby_deploy
• Includes BS files
• Unpacks Gems
• Compiles source
![Page 27: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/27.jpg)
I wanna be an App store billionaire...
![Page 28: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/28.jpg)
I wanna be an App store billionaire...
QuickAlarm
Briquette
Timey
Zero2Nine
![Page 29: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/29.jpg)
MacRuby Nuts & Bolts
![Page 30: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/30.jpg)
Garbage Collector:libauto
• Yet another open source project from Apple
• The GC for Obj-C
• Based on write barriers
• scanning, conservative, generational, multi-threaded garbage collector...
• ...unfortunately...slow
![Page 31: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/31.jpg)
Garbage Collector:the Next Generation(?)
• Ruby does A LOT of allocations
• Nice to not have to stop the world, but...
• Write barriers are slow
• Maybe ref counting?
• Maybe MRI’s collector isn’t so bad after all...
![Page 32: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/32.jpg)
RegExps:ICU
• Oniguruma is not (real thread) thread safe
• ICU is
• thread safe
• Unicode compatible
• not as crazy feature-full as Oniguruma
• There are some bugs...but honestly, stop doing that with RegExps!!!
![Page 33: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/33.jpg)
Hash method syntax
• Ruby 1.9’s hash syntax with Symbol keys:a = { foo: 1, bar: 2, baz: “Hello, world” }
• ...looks a bit like:
[@”Hello, world” rangeOfString:@”ello” options: NSCaseInsensitiveSearch]
• Let’s go with that!
![Page 34: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/34.jpg)
Hash method syntax
• MacRuby turns method calls with hash arguments into SEL
• MacRuby turns method definitions with hash lists into SEL
• MacRuby can call Objective-C
• Objective-C can call MacRuby, but when possible you should use:[[MacRuby sharedRuntime] performRubySelector:@”foo”]
![Page 35: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/35.jpg)
LLVM, JIT, and Hacking
• LLVM provides optimizations
• LLVM provides JIT utilities (kinda slow)
• MacRuby can emit LLVM...VM_DUMP_IR=1 macruby -e "def foo; puts 'hello'; end; foo"
• Find other neat tricks in HACKING.rdoc
![Page 36: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/36.jpg)
Demo
![Page 37: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/37.jpg)
GCD & The Future
![Page 38: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/38.jpg)
GCD
• MacRuby can tap directly into GCD
• Dispatch module:
• Dispatch::Queue
• Dispatch::Group
• Dispatch::Source
• Dispatch Gem too!
![Page 39: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/39.jpg)
GCD
• Dispatch is a powerful tool for concurrent programming
• Example: ControlTower
• MacRuby Rack-based server
• Uses GCD to handle requets
• Accomplishes in 1 line what takes Thin/EventMachine > 6000 lines of C++ & Ruby
![Page 40: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/40.jpg)
GCD
![Page 41: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/41.jpg)
GCD
• Can we make GCD work in MRI?
• JRuby thinks they can make it work...
https://github.com/headius/jcd
• GCD is Multithreaded Programming, Ruby Style
![Page 42: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/42.jpg)
Who Is MacRuby?• Laurent Sansonetti
• Vincent Isambart
• Eloy Durán
• Matt Aimonetti
• Joshua Ballanco
• Thibault Martin-Lagardette
• Watson
• Takao Kouji
• Mark Rada
• You?
![Page 43: MacRuby](https://reader036.vdocument.in/reader036/viewer/2022062418/5555a791d8b42afe5d8b45a4/html5/thumbnails/43.jpg)
Thank you!
http://ofps.oreilly.com/titles/9781449380373/