SlideShare a Scribd company logo
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

More Related Content

PDF
Splash
PDF
Invokedynamic in 45 Minutes
PDF
An Introduction to Scala - Blending OO and Functional Paradigms
PPTX
Just entity framework
PDF
Clojure - An Introduction for Lisp Programmers
PDF
Clojure talk at Münster JUG
PDF
Clojure - An Introduction for Java Programmers
PDF
Miles Sabin Introduction To Scala For Java Developers
Splash
Invokedynamic in 45 Minutes
An Introduction to Scala - Blending OO and Functional Paradigms
Just entity framework
Clojure - An Introduction for Lisp Programmers
Clojure talk at Münster JUG
Clojure - An Introduction for Java Programmers
Miles Sabin Introduction To Scala For Java Developers

Viewers also liked (20)

PPT
Kansas sights
PPT
New  Forodhani ( Jubilee Garden ) Being Rebuilt
PDF
Bio - Jean Fares Couture
PPS
PPS
Father
PPS
Mahatma Gandhi
PPT
Vegetarian vision
PPT
Adjective Jingle
PDF
Transcript - Facebook Marketing Success Summit Ads Presentation 2011
PPT
Schloss mirabell
PPTX
The broiler hen
PDF
Porting the QALL-ME framework to Romanian
PDF
ExL Pharma Clinical Trials Phase I and Phase IIa Conference Brochure: Phase 1...
PDF
Milieu Problematiek
PDF
Facebook Marketing Success Summit Presentation 2011 - Success with Facebook Ads
PPS
Mother
PPT
EuP Directive About Non Directional Domestic Lighting
PPS
10 words
PPTX
July 10th 2014 - to use with members from multiple Unions
PPS
Woman De John Lennon
Kansas sights
New  Forodhani ( Jubilee Garden ) Being Rebuilt
Bio - Jean Fares Couture
Father
Mahatma Gandhi
Vegetarian vision
Adjective Jingle
Transcript - Facebook Marketing Success Summit Ads Presentation 2011
Schloss mirabell
The broiler hen
Porting the QALL-ME framework to Romanian
ExL Pharma Clinical Trials Phase I and Phase IIa Conference Brochure: Phase 1...
Milieu Problematiek
Facebook Marketing Success Summit Presentation 2011 - Success with Facebook Ads
Mother
EuP Directive About Non Directional Domestic Lighting
10 words
July 10th 2014 - to use with members from multiple Unions
Woman De John Lennon
Ad

Similar to Developing Cocoa Applications with macRuby (20)

ZIP
MacRuby to The Max
KEY
Mac ruby to the max - Brendan G. Lim
PDF
Charla ruby nscodermad
ZIP
Why MacRuby Matters
PDF
ruby-cocoa
PDF
ruby-cocoa
PDF
MacRuby & HotCocoa
PDF
MacRuby - When objective-c and Ruby meet
KEY
MacRuby, an introduction
KEY
Modified "Why MacRuby Matters"
PDF
Ruby Meets Cocoa
PDF
MacRuby on Rails
PDF
Mac ruby deployment
KEY
MacRuby: What is it? and why should you care?
PDF
RubyならMacでしょう
PDF
MacRuby For Ruby Developers
PDF
Macruby - RubyConf Presentation 2010
PDF
MacRuby
PDF
Macruby& Hotcocoa presentation by Rich Kilmer
PDF
RVM and Ruby Interpreters @ RSC Roma 03/2011
MacRuby to The Max
Mac ruby to the max - Brendan G. Lim
Charla ruby nscodermad
Why MacRuby Matters
ruby-cocoa
ruby-cocoa
MacRuby & HotCocoa
MacRuby - When objective-c and Ruby meet
MacRuby, an introduction
Modified "Why MacRuby Matters"
Ruby Meets Cocoa
MacRuby on Rails
Mac ruby deployment
MacRuby: What is it? and why should you care?
RubyならMacでしょう
MacRuby For Ruby Developers
Macruby - RubyConf Presentation 2010
MacRuby
Macruby& Hotcocoa presentation by Rich Kilmer
RVM and Ruby Interpreters @ RSC Roma 03/2011
Ad

More from Brendan Lim (6)

KEY
Introduction to Palm's Mojo SDK
PDF
Building Native Apps With Titanium Mobile
PDF
Im Mobile Who's Coming With Me
KEY
The Lure Of Ubiquitous Mobile
KEY
Mobilizing Your Rails Application - Rails Underground, London, UK
PPT
Mobilizing Your Rails Application - LA Ruby Conference 2009
Introduction to Palm's Mojo SDK
Building Native Apps With Titanium Mobile
Im Mobile Who's Coming With Me
The Lure Of Ubiquitous Mobile
Mobilizing Your Rails Application - Rails Underground, London, UK
Mobilizing Your Rails Application - LA Ruby Conference 2009

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
A Presentation on Artificial Intelligence
PPTX
Machine Learning_overview_presentation.pptx
PDF
Encapsulation theory and applications.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPT
Teaching material agriculture food technology
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Programs and apps: productivity, graphics, security and other tools
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
NewMind AI Weekly Chronicles - August'25-Week II
Review of recent advances in non-invasive hemoglobin estimation
MYSQL Presentation for SQL database connectivity
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
A Presentation on Artificial Intelligence
Machine Learning_overview_presentation.pptx
Encapsulation theory and applications.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Teaching material agriculture food technology
Assigned Numbers - 2025 - Bluetooth® Document
sap open course for s4hana steps from ECC to s4
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Spectral efficient network and resource selection model in 5G networks
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction
gpt5_lecture_notes_comprehensive_20250812015547.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”

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 desktop apps? Saturday, February 5, 2011
  • 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
  • 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
  • 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 - Gem Support • 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 will we be using? Saturday, February 5, 2011
  • 38. Let’s build a MacRuby 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 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
  • 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