Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

MacRuby to The Max

8,915 views

Published on

Introduction and overview of MacRuby. Presentation given at RubyConf India 2010 in Bangalore, India.

Published in: Technology
  • Be the first to comment

MacRuby to The Max

  1. MAC RUBY Brendan G. Lim @brendanlim brendan@intridea.com
  2. WHO HERE HAS A MAC?
  3. WHO HERE HAS WORKED WITH OBJECTIVE-C?
  4. DEVELOPED OSX OR IPHONE APPS?
  5. OBJECTIVE-C • Object-oriented extensions to C • Strongly typed • Like Ruby, influenced by Smalltalk • Primary language for Cocoa • Garbage collection (2.0) • 32 and 64-bit support
  6. COCOA • Set of object-oriented frameworks • Used for writing Max OSX and iPhone OS apps • Includes FoundationKit, and AppKit • Typically built using tools like XCode and Interface Builder
  7. CORE FOUNDATION • Low level libraries that ship with Mac OSX • Used for passing primitive types to OSX C routines • Most routines follow ‘CF’ naming convention • Open-Sourced by Apple as CF-Lite
  8. WHY RUBY INSTEAD OF OBJECTIVE-C?
  9. RUBY VS OBJECTIVE-C object.method(param) = [object method:param];
  10. RUBY VS OBJECTIVE-C person.set_name(“john”, “doe”) = [person setNameWithFirst:@”john” withLast:@”doe”];
  11. RUBY VS OBJECTIVE-C array = [] = NSMutableArray *array = [[NSMutableArray alloc] init];
  12. RUBY VS OBJECTIVE-C “ string”.strip = [@“ string” stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]
  13. RUBY VS OBJECTIVE-C dictionary = {“key1” => “value1”, “key2” => “value2”} = NSArray *keys = [NSArray arrayWithObjects:@”key1”,@”key2”]; NSArray *data = [NSArray arrayWithObjects:@”value1”,@”value2”]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
  14. RUBYCOCOA • Mac OSX framework • Bridge between Objective-C and Ruby • Can manipulate Objective-C objects using Ruby • Write Cocoa apps in Ruby • Runs on Ruby 1.8 • Ships with OSX Leopard
  15. RUBYCOCOA VS OBJECTIVE-C setObject_forKey_(iphone, phone) = [setObject:iphone forKey:phone];
  16. SO WHY NOT RUBYCOCOA?
  17. WHY NOT RUBYCOCOA • Performance • Two runtimes • Two garbage collectors • Object conversions • Syntax doesn’t feel like Ruby
  18. MAC RUBY +
  19. MAC RUBY • Impl. of Ruby 1.9 that runs on the Objective-C runtime • Open sourced by Apple • Originally created by Laurent Sansonetti • Replacing RubyCocoa • Objects are peers with no translation layer • Multi-core (Grand Central Dispatch) • HotCocoa Support
  20. MAC RUBY Object NSObject String NSMutableString Number NSNumber Array NSMutableArray Hash NSMutableDictionary
  21. MAC RUBY Objects Objective-C Classes Objective-C Methods Objective-C
  22. MAC RUBY >> s = “rubyconf” => “rubyconf” >> s.class => NSMutableString >> s.class.ancestors => [NSMutableString,NSString,Comparable,NSObject,Kernel] >> s.upcase => “RUBYCONF” >> s.uppercaseString => “RUBYCONF”
  23. MAC RUBY >> NSString.new(“rubyconf”) => “rubyconf” >> NSString.stringWithString(“rubyconf”) => “rubyconf” >> NSString.alloc.initWithString(“rubyconf”) => “rubyconf”
  24. MAC RUBY >> a = [] => [] >> a.class => NSMutableArray >> a.class.ancestors => [NSMutableArray,NSArray,Enumerable,NSObject,Kernel] >> a << “RubyConf” => [“RubyConf”]
  25. MACRUBY VS OBJECTIVE-C -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { }
  26. MACRUBY VS OBJECTIVE-C def locationManager(manager didUpdateToLocation:newLocation fromLocation:oldLocation) end -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { }
  27. MACRUBY VS OBJECTIVE-C def locationManager(manager didUpdateToLocation:newLocation fromLocation:oldLocation) end >> locationManager(manager didUpdateToLocation:newLocation, fromLocation:oldLocation)
  28. MACRUBY VS OBJECTIVE-C def locationManager(manager didUpdateToLocation:newLocation fromLocation:oldLocation) end >> locationManager(manager :didUpdateToLocation => newLocation, :fromLocation => oldLocation)
  29. MACRUBY VS OBJECTIVE-C Interface Builder Outlets attr_accessor :my_string = # Interface (header file) NSString *myString; @property(nonatomic,retain) IBOutlet NSString *myString;
  30. MACRUBY VS OBJECTIVE-C Interface Builder Actions def my_action(sender) ... end = # Interface -(IBAction) myAction:(id)sender; # Implementation -(IBAction) myAction:(id)sender { ... }
  31. WAIT, WHAT ABOUT MACRUBY FOR IPHONE/IPAD?
  32. MAC RUBY Xcode
  33. MAC RUBY Interface Builder
  34. MAC RUBY Instruments
  35. MAC RUBY Let’s build something ...
  36. TDD IN MACRUBY • Good supports for tests • AnyRuby testing framework instantly becomes an Objective-C testing framework • Test::Unit • RSpec • Cucumber • etc...
  37. HOTCOCOA • Use Ruby to build a UI without Interface Builder • Developed by Rich Kilmer • MacRuby 0.5 now supports HotCocoa • Packaged as a gem in MacRuby 0.5
  38. MACRUBY VS HOTCOCOA win = NSWindow.alloc.initWithContentRect([10,20,300,300], :styleMask => (NSTitleWindowMask | NSCloseableWindowMask | NSMiniatureizableWindowMask | NSResizeableWindowMask) = win = window :frame => [10,20,300,300]
  39. HELLO WORLD IN HOTCOCOA require ‘rubygems’ require ‘hotcocoa’ include HotCocoa application do |app| win = window :size => [100,50] b = button :title => ‘Hello’ b.on_action { puts ‘World!’ } win << b end
  40. MAC RUBY Very exciting future
  41. MAC RUBY It’s Ruby for Cocoa Apps!
  42. MAC RUBY Questions? http://macruby.org http://macruby.org/hotcocoa.html http://bit.ly/macruby-getting-started http://bit.ly/macruby-examples http://bit.ly/tdd-macruby http://github.com/masterkain/macruby

×