Contenu connexe Similaire à Macruby intro Similaire à Macruby intro (20) Macruby intro1. MacRuby
SHRUG 2011-02-22
Peter Lind @peter_lind
2. What?
• Ruby 1.9 on the Objective-C runtime
• Apple project with external contributors
• Ruby License
3. Why?
• Better runtime
• GC, JIT compilation, AOT compilation
• Cocoa integration
• Ruby classes extends NSString, NSArray
etc
4. How?
• macruby / macirb / macgem / macrake
• require ‘rubygems’
• framework ‘cocoa’
5. Benefits
• No GIL
• Multithreaded GC
• lib_dispatch
• Access to Cocoa APIs
• GrandCentral, Core Animation, Speech
Recognition, Core Location
6. Micro-benchmark
~ $ rvm use 1.8.7
info: Using ruby 1.8.7 p302
~ $ ruby -e "start = Time.now; def fib(inp) inp < 2 ? inp : fib(inp-1) + fib(inp-2); end; (1..30).each do |i| fib(i);
end; puts Time.now - start"
3.097749
~ $ rvm use 1.9.2
info: Using ruby 1.9.2 p0
~ $ ruby -e "start = Time.now; def fib(inp) inp < 2 ? inp : fib(inp-1) + fib(inp-2); end; (1..30).each do |i| fib(i);
end; puts Time.now - start"
0.553159
~ $ rvm use jruby
info: Using jruby 1.5.2
~ $ ruby -e "start = Time.now; def fib(inp) inp < 2 ? inp : fib(inp-1) + fib(inp-2); end; (1..30).each do |i| fib(i);
end; puts Time.now - start"
0.692
~ $ macruby --version
MacRuby 0.8 (ruby 1.9.2) [universal-darwin10.0, x86_64]
~ $ macruby -e "start = Time.now; def fib(inp) inp < 2 ? inp : fib(inp-1) + fib(inp-2); end; (1..30).each do |i| fib
(i); end; puts Time.now - start"
0.19616
8. Ruby vs Objective-C
main_window_controller.rb
class MainWindowController < NSController
attr_accessor :url, :button
def start_stop(sender)
#implementation
end
MainWindowController.h MainWindowController.m
@interface DemoTakeTwoViewController : NSController { #import "DemoTakeTwoViewController.h"
UITextField *url;
UIButton *button; @implementation MainWindowController
}
@synthesize url;
@property (nonatomic, retain) IBOutlet UITextField @synthesize button;
*url;
@property (nonatomic, retain) IBOutlet UIButton - (IBAction)startStop {
*button; //implementation
}
- (IBAction)startStop;
9. • Pro
• Mac AppStore
• Scratch your own itch
• Fast
• Test Objective-C with RSpec
• Call into C or Obj-C code
• Con
• 50 MB download penalty
• Some gems not working (yet)
• iOS
10. Sounds like fun?
• http://macruby.org
• /Developer/Examples/Ruby/MacRuby
• http://macruby.labs.oreilly.com
• http://developer.apple.com
• git://github.com/peterlind/Hello-SHRUG.git