KEMBAR78
Developing Cocoa Applications with macRuby | PDF
Developing Cocoa
                      Applications With

                       MacRuby
                             Brendan G. Lim
                                 @brendanlim
                             brendan@intridea.com


Saturday, February 5, 2011
Outline
                             •   Objective-C & Cocoa
                             •   RubyCocoa
                             •   MacRuby
                             •   Live Coding
                             •   HotCocoa




Saturday, February 5, 2011
Objective -C
                             •   Object-oriented extensions to C
                             •   Strongly typed
                             •   Like Ruby, influenced by Smalltalk
                             •   Primarily used for Mac OS X and iOS




Saturday, February 5, 2011
Cocoa
                    •   High-level API for Mac OS X

                    •   Set of frameworks

                    •   Includes FoundationKit, AppKit, etc.

                    •   Apps typically built using tools like XCode
                        and Interface Builder




Saturday, February 5, 2011
Why make desktop apps?




Saturday, February 5, 2011
Different Paradigm




Saturday, February 5, 2011
Mac App Store


                                  1. Build MacRuby application
                                  2. Submit to App Store
                                  4. Profit




Saturday, February 5, 2011
Why Ruby instead
                              of Objective-C?



Saturday, February 5, 2011
Apple Loves Ruby

                             2002   Mac OS X 10.2           Ruby 1.6.7

                             2005   Mac OS X 10.4           Ruby 1.8.2

                             2007   Mac OS X 10.5          Ruby 1.8.6
                                                        RubyCocoa, RubyGems, Rails


                             2009   Mac OS X 10.6           Ruby 1.8.7
                                                        RubyCocoa, RubyGems, Rails


                             2011   Mac OS X 10.7          Ruby 1.9.x?
                                                    MacRuby? RubyCocoa, RubyGems, Rails




Saturday, February 5, 2011
Ruby vs Objective-C

                              object.method(param)
                                       =

                             [object method:param];




Saturday, February 5, 2011
Ruby vs Objective-C

                              array = []
                                  =
               NSMutableArray *array =
            [[NSMutableArray alloc] init];



Saturday, February 5, 2011
Ruby vs Objective-C

                             “ string”.strip
                                    =
       [@“ string” stringByTrimmingCharactersInSet:
    [NSCharacterSet whitespaceAndNewlineCharacterSet]]




Saturday, February 5, 2011
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];




Saturday, February 5, 2011
+




Saturday, February 5, 2011
RubyCocoa
                •   Bridge between Objective-C and Ruby

                •   Manipulate Objective-C objects using Ruby

                •   Write Cocoa apps in Ruby

                •   Runs on Ruby 1.8

                •   Ships with OSX Leopard



Saturday, February 5, 2011
RubyCocoa vs Objective-C
   OSX::NSNotificationCenter.defaultCenter.addObserver_selector_name_object(
     self,
      :window_moved,
      "NSWindowDidMoveNotification",
      nil
   )

                                      =
   [[NSNotificationCenter defaultCenter] addObserver:self,
     selector:@selector(windowMoved:)
     name:”NSWindowDidMoveNotification”
     object:nil];




Saturday, February 5, 2011
So, Why Not
                             RubyCocoa?
                             (besides it looking gross)




Saturday, February 5, 2011
Why Not RubyCocoa?
                             •   It’s a bridge!

                                 •   Two runtimes, GCs, etc.

                                 •   Object conversions

                             •   Syntax doesn’t feel like idiomatic Ruby

                             •   It’s getting replaced



Saturday, February 5, 2011
+




Saturday, February 5, 2011
MacRuby

              •   Implementation of Ruby 1.9 that runs on top
                  the Objective-C runtime

              •   Open sourced and supported by Apple

              •   Replacing RubyCocoa

              •   Objects are peers with no translation layer



Saturday, February 5, 2011
MacRuby
                             Object     NSObject
                             String     NSMutableString
                             Number     NSNumber
                             Array      NSMutableArray
                             Hash       NSMutableDictionary




Saturday, February 5, 2011
MacRuby

                             Objects   Objective-C
                             Classes   Objective-C
                             Methods   Objective-C




Saturday, February 5, 2011
MacRuby
     >> s = “magicruby”
     => “magicruby”

     >> s.class
     => String

     >> s.class.ancestors
     => [String,NSMutableString,NSString,Comparable,NSObject,Kernel]

     >> s.upcase
     => “MAGICRUBY”

     >> s.uppercaseString
     => “MAGICRUBY”




Saturday, February 5, 2011
MacRuby

     >> NSString.new(“magicruby”)
     => “magicruby”

     >> NSString.stringWithString(“magicruby”)
     => “magicruby”

     >> NSString.alloc.initWithString(“magicruby”)
     => “magicruby”




Saturday, February 5, 2011
MacRuby
     >> a = []
     => []

     >> a.class
     => Array

     >> a.class.ancestors
     => [Array,NSMutableArray,NSArray,Enumerable,NSObject,Kernel]

     >> a << “MagicRuby”
     => [“MagicRuby”]




Saturday, February 5, 2011
MacRuby vs Objective-C
      -(id)       tableView:(NSTableView *)tableView
       objectValueForColumn:(NSTableColumn *)column
                        row:(int)rowIndex { .. }




Saturday, February 5, 2011
MacRuby vs Objective-C
      -(id)       tableView:(NSTableView *)tableView
       objectValueForColumn:(NSTableColumn *)column
                        row:(int)rowIndex { .. }




         def tableView(tableView
               objectValueForColumn:column
               row:rowIndex)
         end



Saturday, February 5, 2011
MacRuby vs Objective-C
                             Interface Builder Outlets & Actions




                                                                   !




Saturday, February 5, 2011
MacRuby vs Objective-C
                                Interface Builder Outlets

         # Interface
         NSString *myString;
         @property(nonatomic,retain) IBOutlet NSString *myString;


                                           =

                             attr_accessor :myString




Saturday, February 5, 2011
MacRuby vs Objective-C
                              Interface Builder Actions

             # Implementation
             -(IBAction) myAction:(id)sender { ... }


                                         =

                             def myAction(sender)
                              ...
                             end


Saturday, February 5, 2011
MacRuby - Gem Support

                             •   sudo macgem install awesome_gem

                             •   Not all gems supported right now




Saturday, February 5, 2011
MacRuby - Objective-C
                      Frameworks & Libraries

              •   Libraries must have garbage collection support

              •   Libraries must be turned into bundles

              •   Frameworks can easily be included




Saturday, February 5, 2011
Testing MacRuby

                 •   Any Ruby testing framework instantly becomes
                     an Objective-C testing framework

                     •   Test::Unit

                     •   RSpec

                     •   etc...



Saturday, February 5, 2011
What tools will we
                                be using?



Saturday, February 5, 2011
Xcode



Saturday, February 5, 2011
Interface Builder



Saturday, February 5, 2011
Instruments

Saturday, February 5, 2011
Let’s build a MacRuby app




Saturday, February 5, 2011
HotCocoa

               •    Created by Rich Kilmer

               •    Ruby layer that sits on top of Cocoa, etc.

               •    Use Ruby to easily create user interfaces

               •    Used to be included with MacRuby

               •    Now available as a gem



Saturday, February 5, 2011
HotCocoa
                win = NSWindow.alloc.initWithContentRect([10,20,300,300],
                   styleMask: (NSTitleWindowMask           |
                               NSCloseableWindowMask       |
                               NSMiniatureizableWindowMask |
                               NSResizeableWindowMask)



                                           =

                win = window :frame => [10,20,300,300]




Saturday, February 5, 2011
HotCocoa

                             sudo macgem install hotcocoa

                               hotcocoa NAME_OF_APP




Saturday, February 5, 2011
Hello World in HotCocoa
    require ‘hotcocoa’

    class Application
      include HotCocoa

      def start
        application :name => "Hello" do |app|
          app.delegate = self
          window :frame => [500,500,200,100], :title => "Hello" do |win|
            win << label(:text => "Hello World",:layout => {:start => false})
            win.will_close { exit }
          end
        end
      end
    end

    Application.new.start



Saturday, February 5, 2011
Questions?

                                         MacRuby in Action
                                         http://manning.com/lim




                                       http://macruby.org
                             http://bit.ly/macruby-getting-started
                                http://bit.ly/macruby-examples
                                   http://bit.ly/tdd-macruby
Saturday, February 5, 2011

Developing Cocoa Applications with macRuby

  • 1.
    Developing Cocoa Applications With MacRuby Brendan G. Lim @brendanlim brendan@intridea.com Saturday, February 5, 2011
  • 2.
    Outline • Objective-C & Cocoa • RubyCocoa • MacRuby • Live Coding • HotCocoa Saturday, February 5, 2011
  • 3.
    Objective -C • Object-oriented extensions to C • Strongly typed • Like Ruby, influenced by Smalltalk • Primarily used for Mac OS X and iOS Saturday, February 5, 2011
  • 4.
    Cocoa • High-level API for Mac OS X • Set of frameworks • Includes FoundationKit, AppKit, etc. • Apps typically built using tools like XCode and Interface Builder Saturday, February 5, 2011
  • 5.
    Why make desktopapps? Saturday, February 5, 2011
  • 6.
  • 7.
    Mac App Store 1. Build MacRuby application 2. Submit to App Store 4. Profit Saturday, February 5, 2011
  • 8.
    Why Ruby instead of Objective-C? Saturday, February 5, 2011
  • 9.
    Apple Loves Ruby 2002 Mac OS X 10.2 Ruby 1.6.7 2005 Mac OS X 10.4 Ruby 1.8.2 2007 Mac OS X 10.5 Ruby 1.8.6 RubyCocoa, RubyGems, Rails 2009 Mac OS X 10.6 Ruby 1.8.7 RubyCocoa, RubyGems, Rails 2011 Mac OS X 10.7 Ruby 1.9.x? MacRuby? RubyCocoa, RubyGems, Rails Saturday, February 5, 2011
  • 10.
    Ruby vs Objective-C object.method(param) = [object method:param]; Saturday, February 5, 2011
  • 11.
    Ruby vs Objective-C array = [] = NSMutableArray *array = [[NSMutableArray alloc] init]; Saturday, February 5, 2011
  • 12.
    Ruby vs Objective-C “ string”.strip = [@“ string” stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]] Saturday, February 5, 2011
  • 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]; Saturday, February 5, 2011
  • 14.
  • 15.
    RubyCocoa • Bridge between Objective-C and Ruby • Manipulate Objective-C objects using Ruby • Write Cocoa apps in Ruby • Runs on Ruby 1.8 • Ships with OSX Leopard Saturday, February 5, 2011
  • 16.
    RubyCocoa vs Objective-C OSX::NSNotificationCenter.defaultCenter.addObserver_selector_name_object( self, :window_moved, "NSWindowDidMoveNotification", nil ) = [[NSNotificationCenter defaultCenter] addObserver:self, selector:@selector(windowMoved:) name:”NSWindowDidMoveNotification” object:nil]; Saturday, February 5, 2011
  • 17.
    So, Why Not RubyCocoa? (besides it looking gross) Saturday, February 5, 2011
  • 18.
    Why Not RubyCocoa? • It’s a bridge! • Two runtimes, GCs, etc. • Object conversions • Syntax doesn’t feel like idiomatic Ruby • It’s getting replaced Saturday, February 5, 2011
  • 19.
  • 20.
    MacRuby • Implementation of Ruby 1.9 that runs on top the Objective-C runtime • Open sourced and supported by Apple • Replacing RubyCocoa • Objects are peers with no translation layer Saturday, February 5, 2011
  • 21.
    MacRuby Object NSObject String NSMutableString Number NSNumber Array NSMutableArray Hash NSMutableDictionary Saturday, February 5, 2011
  • 22.
    MacRuby Objects Objective-C Classes Objective-C Methods Objective-C Saturday, February 5, 2011
  • 23.
    MacRuby >> s = “magicruby” => “magicruby” >> s.class => String >> s.class.ancestors => [String,NSMutableString,NSString,Comparable,NSObject,Kernel] >> s.upcase => “MAGICRUBY” >> s.uppercaseString => “MAGICRUBY” Saturday, February 5, 2011
  • 24.
    MacRuby >> NSString.new(“magicruby”) => “magicruby” >> NSString.stringWithString(“magicruby”) => “magicruby” >> NSString.alloc.initWithString(“magicruby”) => “magicruby” Saturday, February 5, 2011
  • 25.
    MacRuby >> a = [] => [] >> a.class => Array >> a.class.ancestors => [Array,NSMutableArray,NSArray,Enumerable,NSObject,Kernel] >> a << “MagicRuby” => [“MagicRuby”] Saturday, February 5, 2011
  • 26.
    MacRuby vs Objective-C -(id) tableView:(NSTableView *)tableView objectValueForColumn:(NSTableColumn *)column row:(int)rowIndex { .. } Saturday, February 5, 2011
  • 27.
    MacRuby vs Objective-C -(id) tableView:(NSTableView *)tableView objectValueForColumn:(NSTableColumn *)column row:(int)rowIndex { .. } def tableView(tableView objectValueForColumn:column row:rowIndex) end Saturday, February 5, 2011
  • 28.
    MacRuby vs Objective-C Interface Builder Outlets & Actions ! Saturday, February 5, 2011
  • 29.
    MacRuby vs Objective-C Interface Builder Outlets # Interface NSString *myString; @property(nonatomic,retain) IBOutlet NSString *myString; = attr_accessor :myString Saturday, February 5, 2011
  • 30.
    MacRuby vs Objective-C Interface Builder Actions # Implementation -(IBAction) myAction:(id)sender { ... } = def myAction(sender) ... end Saturday, February 5, 2011
  • 31.
    MacRuby - GemSupport • sudo macgem install awesome_gem • Not all gems supported right now Saturday, February 5, 2011
  • 32.
    MacRuby - Objective-C Frameworks & Libraries • Libraries must have garbage collection support • Libraries must be turned into bundles • Frameworks can easily be included Saturday, February 5, 2011
  • 33.
    Testing MacRuby • Any Ruby testing framework instantly becomes an Objective-C testing framework • Test::Unit • RSpec • etc... Saturday, February 5, 2011
  • 34.
    What tools willwe be using? Saturday, February 5, 2011
  • 35.
  • 36.
  • 37.
  • 38.
    Let’s build aMacRuby app Saturday, February 5, 2011
  • 39.
    HotCocoa • Created by Rich Kilmer • Ruby layer that sits on top of Cocoa, etc. • Use Ruby to easily create user interfaces • Used to be included with MacRuby • Now available as a gem Saturday, February 5, 2011
  • 40.
    HotCocoa win = NSWindow.alloc.initWithContentRect([10,20,300,300], styleMask: (NSTitleWindowMask | NSCloseableWindowMask | NSMiniatureizableWindowMask | NSResizeableWindowMask) = win = window :frame => [10,20,300,300] Saturday, February 5, 2011
  • 41.
    HotCocoa sudo macgem install hotcocoa hotcocoa NAME_OF_APP Saturday, February 5, 2011
  • 42.
    Hello World inHotCocoa require ‘hotcocoa’ class Application include HotCocoa def start application :name => "Hello" do |app| app.delegate = self window :frame => [500,500,200,100], :title => "Hello" do |win| win << label(:text => "Hello World",:layout => {:start => false}) win.will_close { exit } end end end end Application.new.start Saturday, February 5, 2011
  • 43.
    Questions? MacRuby in Action http://manning.com/lim http://macruby.org http://bit.ly/macruby-getting-started http://bit.ly/macruby-examples http://bit.ly/tdd-macruby Saturday, February 5, 2011