2. Overview
About - Web developer - macruby - - Upstream - Alex - Web developer - WebApplications - doningtext
- forex-metal - In business for a while - bootstrap - Paper-C - fast - rails - ruby - reliable - testing - rspec
- cucumber - ruby - reliable - macs - ruby - rails - text mate - fast - plugins - gems - tools - interesting
techs - iphone - mac - tools - Objective-C - features like ruby - smalltalk - more code - Hello World
Example - not so fast - unlike ruby - dropped it - heard about macruby 0.3 relase - on top of Obj-C -
get curious - less code - on Mac - with Cocoa - major API - Desktop Applications - More like ruby -
liked it - Its from Apple! - from - RubyWrapper for NSObject 2001 - Integration with ProjectBuilder (IB
2002) - 2006 Laurent Sansonetti - 10.5 shipped with OS X - 2008 MacRuby shall replace RubyCocoa -
RubyCocoa is a scripting Bridge - Syntactic Sugar - Syntax Mismatch - 2 Object Graphs - Green
Threads vs. native Threads - Ruby 1.8 and ObjectiveC - Standard Libs Broken - Slow!!! - Mismatch like
String != NSString => more Code - Picture - Ruby first Class Citizen - MacRuby - Ruby 1.9 - Native
Threads - Matches better with Obj Syntax - Faster - Tighter Integration with Obj-C - Share Base classes
- Share Infrastructure - GC - MacRuby 0.5 - the best Ruby for Mac - In Development - NO GLI - Share
IO - On top of LLVM - Doing optimizations - Calls Obj-C - class/methods/obj all Obj C - show Methods
- No more libffi but uses a new C-dispatcher - Picture! - Show Mixing MacRuby with ObjC - Compiles
Code JIT - soon AOT - other Platforms - iPhone? - IB integration! - Framework - main_rm - Outlet -
Action - Delegates - Like ActiveRecord Callbacks - but fixed names - you tell who handle it - first
responder - Delegate for Application - Show Random Number Gen with IB - Code Obj-C/MacRuby-
All the things I’d like to talk about.
3. Outline
Intro
Objective-C Encounter
Apple - Ruby
RubyCocoa
MacRuby 0.5
MacRuby <-> Obj-C
Interface Builder Integration
Back to Code with HotCocoa
Impact
25. First Objective-C App
class Foo
attr_writer :text_field
def seed(sender)
@text_field.StringValue = quot;Generator doesn't need to be seeded ;)quot;
end
def generate(sender)
@text_field.StringValue = (rand(100) + 1)
end
end
Same Obj-C Tutorial App with MacRuby.
35. RubyCocoa
Use Objects from Obj-C in Ruby and vice versa
Syntax matching
[Class initWith:(type) p1 other:(type) p2]
Frameworks
Class.initWith_other(p1, p2)
Extending/Inheriting/Overwriting
App Std Lib Obj-C
Access to C-Structs
Ruby Code GC GC IO GCC
Bridge
MRI 1.8 Obj-C Runtime
36. RubyCocoa
Messaging between Obj-C and Ruby is slow!
Frameworks
App Std Lib Obj-C
Ruby Code GC GC IO GCC
Bridge
MRI 1.8 Obj-C Runtime
37. RubyCocoa
Two Object Spaces
quot;Hiquot; != @quot;Hiquot;
Green Threads vs. Native Threads Frameworks
App Std Lib Obj-C
Ruby Code GC GC IO GCC
Bridge
MRI 1.8 Obj-C Runtime
RubyCocoa can run only on the MainThread.
Ruby needs patching to allow to work with the Obj-C runtime.
41. 2008
MacRuby
shall replace RubyCocoa
in the future
The appearance of MacRuby
42. 2009
MacRuby gets popular
Ruby 1.8.7, Rails 2.3, RubyGems
and RubyCocoa
on OSX 10.6
Community starts to grow.
Gets buzz, because it for example faster than YARV.
Appears more in screencasts/conferences.
Apple Stayes true to ruby
45. ./miniruby -e 'p Array.new.methods(true, true)'
[:quot;sortUsingFunction:context:quot;, :quot;exchangeObjectAtIndex:withObjectA
tIndex:quot;, :removeAllObjects, :quot;getObjects:range:quot;, :quot;containsObject
:inRange:quot;, :removeObjectsInRange, :removeObjectAtIndex, :removeLas
tObject, :quot;replaceObjectAtIndex:withObject:quot;, :quot;insertObject:atInde
x:quot;, :addObject, :quot;countByEnumeratingWithState:objects:count:quot;, :ob
jectAtIndex, :count, :mutableCopyWithZone, :copyWithZone, :classFor
Coder, :finalize, :hash, :isEqual, :sortUsingDescriptors, :filterUs
ingPredicate, :initWithContentsOfURL, :initWithContentsOfFile, :sor
tUsingSelector, :quot;removeObjectsFromIndices:numIndices:quot;, :removeObj
ectsAtIndexes, :quot;replaceObjectsAtIndexes:withObjects:quot;, :quot;insertObj
ects:atIndexes:quot;, :quot;sortUsingFunction:context:range:quot;, :quot;initWithOb
jects:count:quot;, :initWithCapacity, :removeObjectsInArray, :quot;removeOb
jectIdenticalTo:inRange:quot;, :removeObjectIdenticalTo, :quot;removeObject
:inRange:quot;, :removeObject, :quot;removeObject:range:identical:quot;, :setAr
ray, :quot;setObject:atIndex:quot;, :addObjectsFromArray, :quot;replaceObjectsI
With the extra second argument set on true, you can see the Obj-C methods.
46. MacRuby 0.5
Generate Obj-C calls for Ruby code
Intermediate Representation (IR)
App Std Lib
Optimization Frameworks
Ruby Code
JIT compiler to execute code
GCC
LLVM ~ 3 x faster than YARV
JIT/AOT GC IO LLVM
AOT compiler very soon
Obj-C Runtime
LLVM = Low Level Virtual Machine
47. MacRuby 0.5
Generate Obj-C calls for Ruby code
Intermediate Representation (IR)
App Std Lib
Optimization Frameworks
Ruby Code
JIT compiler to execute code
GCC
LLVM ~ 3 x faster than YARV
JIT/AOT GC IO LLVM
AOT compiler very soon
Obj-C Runtime
AOT Compiler might allow to build for the iPhone. The garbage collection is still an open
question.
49. MacRuby 0.5
One runtime
App One Lib
Std object space
Frameworks
Shared infrastructure
Ruby Code
Multithreaded Obj-C
LLVM
JIT/AOT GC IO GCC
Obj-C Runtime
No GIL is needed.
50. MacRuby 0.5
MacRuby is not fully functional yet.
Not installable around 85% RubySpecs passes.
You can play with miniruby.
56. Pointer
NSError* error;
//MacRuby 0.4
error = Pointer.new_with_type('@')
//MacRuby 0.5
error = Pointer.new(:object)
Pointers are often used as a means for output parameters, mostly for error handling.
58. Use Ruby from Objective-C
//Possible
id dog = [NSClassFromString(@quot;Dogquot;) new];
[dog bark]
//Safer
id dog = [[MacRuby sharedRuntime]
evaluateString:@quot;Dog.newquot;];
[dog performRubySelector: @selector(bark:)];
Ruby Parameters are always objects, careful when passing primitives!
71. def applicationDidFinishLoading(message)
// do somehting here
end
I am your delegate
Answer my Emails!
Alternative explanation: ActiveRecord callbacks with fixed names defined in another object.
72. With InterfaceBuilder you get very much, very fast, with very little code but rather static.
Good for small Apps, prototypes, App parts.
77. HotCocoa
Constructors with smart defaults
Constants mapped to Symbols
Custom methods
Easy delegation
Constructors- Building the instances of the mapped classes using the correct class-specific
APIs
Smart Defaults - Default constructor parameters to minimize the needed parameters
Constant Mapping - Use of Ruby symbols to minimize the text and incr. readability
Custom Methods- Ruby-friendly API for commonly used methods
Delegate Methods - Ruby-friendly methods for delegating instances
78. Hello World HotCocoa
require 'hotcocoa'
include HotCocoa
application do
window = window(title: 'Hello World', frame: [0,0,200,60])
label = label(text:'Hello RailsWayCon!', layout: {align::center})
window << label
end
Same App done with MacRuby.