• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
MacRuby to The Max
 

MacRuby to The Max

on

  • 8,791 views

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

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

Statistics

Views

Total Views
8,791
Views on SlideShare
8,753
Embed Views
38

Actions

Likes
13
Downloads
85
Comments
0

5 Embeds 38

http://www.slideshare.net 34
http://www.kapilmohan.com 1
http://kapilmohan.com 1
http://webcache.googleusercontent.com 1
https://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • - Objective-C is the primary language for Cocoa development. If you&#x2019;ve ventured into Mac OSX, iPhone or iPad development you&#x2019;ve been faced with the decision to learn Objective-C. <br /> - Objective-C is essentially object oriented extensions to the C language <br /> - It&#x2019;s a strongly typed language and is influenced by Smalltalk like Ruby -- Objective-C also has Smalltalk-like messaging <br /> - There&#x2019;s garbage collection in Objective-C 2.0 and also there&#x2019;s also 32-bit and 64-bit support <br />
  • - Cocoa is a set of object-oriented frameworks used to create Max OSX and iPhone OS applications. <br /> - It includes two major frameworks, Foundation and AppKit. <br /> - AppKit contains all the objects you need to implement your graphical user interface. <br /> - Foundation includes a base layer of classes that can be used for any type of Cocoa app. <br /> - Cocoa apps are typically developed using Xcode and Interface builder using Objective-C. <br />
  • - Core Foundation is included in Foundation Kit and is a C API that is a mix of low-level routines and wrapper functions <br /> - The most important use of Core Foundation is for passing its own primitive types to numerous GUI related C routines <br /> - You can recognize a Core Foundation function because the naming almost always starts with CF. Some popular ones are CFRetain and CFRelease for memory management. <br /> - Most of Core Foundation was actually open sourced by Apple as a project called CF-Lite <br />
  • - So what&#x2019;s the big deal with Objective-C anyway? <br /> - Why would we want to use Ruby instead of Objective-C? <br />
  • I&#x2019;m going to go through some differences between Ruby and Objective C. In the following slides, the lines colored in red is ruby code and blue is Objective-C. Here&#x2019;s one of the most basic examples I can show. <br /> <br /> - Here you can see how we pass a parameter to a method in ruby and in Objective-C. <br /> - Also, if you&#x2019;re wondering what the square braces are, they are for binding a message to an object in Objective-C. <br /> - Not that bad but it gets more complicated. <br />
  • - In Objective-C the colon separates names and arguments. <br /> - It looks like a hash, but it&#x2019;s not and the order for which arguments are passed in matters. <br /> - Also it&#x2019;s good to note that most arguments are identified by name and the first argument is usually included in or attached to the method name. <br />
  • WIthin Objective-C this is how we would create a new array. We are allocating a NSMutableArray then we are initializing it. Here we&#x2019;re starting to see how much more typing we&#x2019;re going to have to do to accomplish the same thing in Objective-C. <br />
  • - In this example you can really see how much more we have to do to just strip the white space from a string. <br />
  • - So, above is how we would create a hash in Ruby and below is how you would have to do, the exact same thing that we can accomplish in one line, in Objective-C. <br /> - Wouldn&#x2019;t it be great if we could use Ruby instead of doing all of this? <br /> - Unless you really really like typing, I don&#x2019;t see how Objective-C could seem more appealing at this point. <br />
  • - So, this brings us to, RubyCocoa. <br />
  • - So that brings us now to RubyCocoa <br /> - RubyCocoa is a MacOSX framework <br /> - It&#x2019;s a bridge between Objective-C and Ruby and it can manipulate Objective-C objects using Ruby <br /> - It runs on Ruby 1.8 and it allows you to simply write Cocoa apps in Ruby. <br /> - Also, it ships standard with OSX Leopard, so if you have Leopard or Snow Leopard on your Mac, you&#x2019;ve got RubyCocoa. <br />
  • - So, this is a comparison between RubyCocoa and Objective-C. <br /> - This example shows the setObject forKey function in Objective-C. <br /> - On top you can see the RubyCocoa implementation, which is more ruby like but we are oddly mixing camelcase with underscores. <br />
  • - So why not RubyCocoa? <br /> - If we&#x2019;ve already got something that allows us to use Ruby to make Cocoa applications, then why should I use MacRuby? <br />
  • - Since RubyCocoa is a bridge between Obj-C and Ruby, that means that performance is obviously going to be sacrificed. <br /> - There are also two runtimes and two garbage collectors. <br /> - Also, there&#x2019;s expensive object conversions. <br /> - For example, the first time you access the NSTableView class, RubyCocoa will retrieve all of the necessary info regarding this class and will create a Ruby class of the same name to act as a proxy on demand. <br />
  • - So, this is where MacRuby comes in. <br /> - MacRuby is set to be the replacement for RubyCocoa. <br /> - So if you&#x2019;re not too much of a fan of Objective-C then you&#x2019;re going to like MacRuby. <br />
  • If you aren&#x2019;t much of an Objective-C fan you&#x2019;re going to be really happy with MacRuby. <br />
  • - MacRuby is Ruby 1.9 ported to run on top of Objective-C runtime. So all classes, methods, and objects are all objective-c. They accomplished this by taking advantage of CoreFoundation. This gives you things like native threads, obj-c&#x2019;s garbage collector, and much more. <br /> - Project originally started by Laurent Sansonetti of Apple, who also helped out with RubyCocoa <br /> - Unlike RubyCocoa, there&#x2019;s no bridge or translation layer. That means little to no performance penalties. This also means that Ruby objects map to Objective-C objects without any extra work from you. <br /> - Since MacRuby 0.5&#x2019;s release, they&#x2019;ve supported Apple&#x2019;s Grand Central Dispatch Library to support multi-core processors. <br /> - MacRuby is supposed to be Apple&#x2019;s replacement for RubyCocoa. <br /> - Also, MacRuby 0.5 now has HotCocoa support, which I&#x2019;ll briefly talk about later. <br /> <br />
  • Originally, Macruby was called &#x201C;Ruby+Objc&#x201D; and was developed by Laurent Sansonetti. He started working on it in 2007. In March 2008 the first public version of MacRuby was released. In September of 2008 0.3 was released, which included the HotCocoa library, which allows you to develop interfaces using ruby. In March of this year MacRuby 0.4 was released. <br />
  • - In MacRuby, Ruby types are nicely backed by foundation equivalents in Objective-C. <br /> - Here you can see the foundation counterparts of these Ruby classes. <br />
  • - Going further, what we need to realize here is that all objects are objective-c objects, all classes are objective-c classes and all methods defined in a Ruby class are registered with the Objective-C runtime. <br /> - There were able to do this because of Core Foundation, which I mentioned before, which defines all of the primitive data types. <br />
  • - Instead of using IRB you use MACIRB, which acts and performs the same way as IRB but for MacRuby. <br /> - So what I&#x2019;m trying to show you here is that you use MacRuby the same way you would use traditional ruby. <br /> <br /> - All the Ruby types are backed by Foundation equivalents. Here you can see that instead of just a regular Ruby String object being shown, we get back an NSMutableString. Ruby classes like String are now just aliases to their Foundation counterparts. <br /> <br /> - Also, it&#x2019;s because of this that we can still call the ruby string method &#x201C;upcase&#x201D; and call the NSMutableString method uppercaseString. <br />
  • - So it&#x2019;s also good to note that you can build objects by directly calling the Objective-C interface. <br /> - Here you can see us creating a new string by calling NSString.new, stringWithString, and allocating and initializing with a string. <br />
  • - Here you can see us easily creating an array just how we&#x2019;re used to. <br /> - Also you scan ee that we get back an NSMutableArray instead of just a Ruby array. <br /> - The Array class descends from Ruby and Cocoa objects as well. <br />
  • - This is an example that shows you what the method definition looks like in objective-c. <br /> - If you&#x2019;re familiar with iPhone development, this is one of the callback methods of CoreLocation. <br /> - We can see here that the data types for each of the arguments that we need to pass in. <br />
  • - The MacRuby version of this example is shown above. <br /> - It&#x2019;s pretty close except that we removed the data types for each argument and it now looks more like idiomatic ruby with named arguments. <br />
  • - If you were to try and call this method from MacRuby you could do the following with the named arguments and variables. <br />
  • - Also, you can even call this method using key-value hash pairs, which could feel much more natural to you. <br /> - And remember that even though it looks like a hash, you have to make sure that the ordering stays the same. <br />
  • - Interface Builder Outlets are essentially instance variables that refer to other objects. <br /> - When building graphical interfaces with our MacRuby application we need to be able to access our instance variables in Interface Builder so that we can make connections between our controller and our view. <br /> - We can do this by using attr_reader, attr_writer, or attr_accessor in MacRuby. In Objective-C we have to do the following below in red to create an IBOutlet in the header file. <br /> <br />
  • - Interface Builder Actions are methods that we want to be called when a user performs a certain action on an object we specify within Interface Builder. Most commonly this would be a button that you could click on. <br /> - Also, by convention it is mandatory to have one parameter for these actions, which is noted above as sender. Sender specified in the method above would be the button that is clicked. <br /> - Without this sender parameter, Interface Builder will not register this action. <br />
  • - After talking about all of the awesomeness of MacRuby some of you are probably wondering if you could ditch Objective-C and just write iPhone or iPad apps using MacRuby. <br /> - Sadly, you can&#x2019;t yet. Apple doesn&#x2019;t allow interpreted code and doesn&#x2019;t support garbage collection for the iPhone OS. <br /> - Hopefully this will change but right now you&#x2019;re going to have to stick with Objective-C. You could though, use tools like Appcelerator Titanium to create native apps using JavaScript. <br /> - If you&#x2019;re interested in hearing about that please come see me after this talk - I&#x2019;d love to talk about it. <br />
  • - With MacRuby development you&#x2019;ll spend a good chunk of time in XCode. <br /> - XCode is an IDE and I&#x2019;m not sure how many people here use it but if you&#x2019;re uncomfortable with it, you can still use your favorite text editor to work on your ruby files but you&#x2019;ll have to come back to XCode to compile your application and run your application. <br /> - Xcode isn&#x2019;t installed by default but it comes on that second disk you get with OSX and it&#x2019;s also available for free online. <br />
  • - You&#x2019;ll also spend a good chunk of your time using Interface Builder, which is part of Xcode. <br /> - Interface builder allows for you to build the interfaces for your cocoa or macruby applications using a graphical user interface. <br /> - The files you work on are called NIB files which are our views. <br />
  • - Also, with XCode you get great tools like Instruments. <br /> - Instruments allows you to profile your app, check for leaks, memory usage, cpu usage, see how many objects you create, and much more. <br /> - You get all of this when using MacRuby. <br />
  • <br />
  • - What&#x2019;s great about MacRuby is that it&#x2019;s also got good support for testing <br /> - Any Ruby testing framework that you&#x2019;d normally use can be used with MacRuby. <br />
  • - So, the last thing I wanted to talk about was HotCocoa. <br /> - HotCocoa was developed by Rich Kilmer and it&#x2019;s a thin Ruby layer that sits on top of Cocoa and other frameworks. <br /> - It was designed to simplify the way you can create user interfaces without using Interface Builder. <br /> - MacRuby 0.5 now supports HotCocoa and it&#x2019;s included in MacRuby. <br />
  • - Even though MacRuby allows us to write Cocoa apps using Ruby, if we want to programmatically create our interface we still have to implement the same Objective-C methods to create our UI and that can look a little nasty. <br /> - Below you can see how much nicer it is to do this using HotCocoa. <br />
  • - This is how simple it is to use HotCocoa to programmatically construct your cocoa application. <br /> - What this does is create a window 100 pixels wide and 50 pixels tall and adds a button with the title &#x2018;Hello&#x2019;. Once you click this button it will spit out &#x2018;World!&#x2019; to your console. <br />
  • - Well, so MacRuby has a very exciting future ahead. <br /> - The current release is only at version 0.5 so there&#x2019;s so much more that will surprise us before 1.0 is released. <br /> - Also, with Apple&#x2019;s backing, soon everybody could be using Ruby to develop their Cocoa Desktop apps -- also possibly even for the iPhone and the iPad. <br />
  • - And simply, it just allows you to use Ruby to develop Cocoa applications. How cool is that? <br />
  • - MacRuby 0.5 is built upon the low level virtual machine (LLVM) in 0.5 but they used to use YARV <br /> - Switched from YARV to LLVM due to YARV&#x2019;s lack of machine code compilation and the global interpreter lock, which prevented true concurrency. <br /> - MacRuby supports Just In Time and Ahead of Time compilation <br /> - AOT compilation makes MacRuby a true Ruby compiler <br /> <br />

MacRuby to The Max MacRuby to The Max Presentation Transcript

  • MAC RUBY Brendan G. Lim @brendanlim brendan@intridea.com
  • WHO HERE HAS A MAC?
  • WHO HERE HAS WORKED WITH OBJECTIVE-C?
  • DEVELOPED OSX OR IPHONE APPS?
  • 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
  • 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
  • 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
  • WHY RUBY INSTEAD OF OBJECTIVE-C?
  • RUBY VS OBJECTIVE-C object.method(param) = [object method:param];
  • RUBY VS OBJECTIVE-C person.set_name(“john”, “doe”) = [person setNameWithFirst:@”john” withLast:@”doe”];
  • RUBY VS OBJECTIVE-C array = [] = NSMutableArray *array = [[NSMutableArray alloc] init];
  • RUBY VS OBJECTIVE-C “ string”.strip = [@“ string” stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]
  • 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];
  • 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
  • RUBYCOCOA VS OBJECTIVE-C setObject_forKey_(iphone, phone) = [setObject:iphone forKey:phone];
  • SO WHY NOT RUBYCOCOA?
  • WHY NOT RUBYCOCOA • Performance • Two runtimes • Two garbage collectors • Object conversions • Syntax doesn’t feel like Ruby
  • MAC RUBY +
  • 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
  • MAC RUBY Object NSObject String NSMutableString Number NSNumber Array NSMutableArray Hash NSMutableDictionary
  • MAC RUBY Objects Objective-C Classes Objective-C Methods Objective-C
  • MAC RUBY >> s = “rubyconf” => “rubyconf” >> s.class => NSMutableString >> s.class.ancestors => [NSMutableString,NSString,Comparable,NSObject,Kernel] >> s.upcase => “RUBYCONF” >> s.uppercaseString => “RUBYCONF”
  • MAC RUBY >> NSString.new(“rubyconf”) => “rubyconf” >> NSString.stringWithString(“rubyconf”) => “rubyconf” >> NSString.alloc.initWithString(“rubyconf”) => “rubyconf”
  • MAC RUBY >> a = [] => [] >> a.class => NSMutableArray >> a.class.ancestors => [NSMutableArray,NSArray,Enumerable,NSObject,Kernel] >> a << “RubyConf” => [“RubyConf”]
  • MACRUBY VS OBJECTIVE-C -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { }
  • MACRUBY VS OBJECTIVE-C def locationManager(manager didUpdateToLocation:newLocation fromLocation:oldLocation) end -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { }
  • MACRUBY VS OBJECTIVE-C def locationManager(manager didUpdateToLocation:newLocation fromLocation:oldLocation) end >> locationManager(manager didUpdateToLocation:newLocation, fromLocation:oldLocation)
  • MACRUBY VS OBJECTIVE-C def locationManager(manager didUpdateToLocation:newLocation fromLocation:oldLocation) end >> locationManager(manager :didUpdateToLocation => newLocation, :fromLocation => oldLocation)
  • MACRUBY VS OBJECTIVE-C Interface Builder Outlets attr_accessor :my_string = # Interface (header file) NSString *myString; @property(nonatomic,retain) IBOutlet NSString *myString;
  • MACRUBY VS OBJECTIVE-C Interface Builder Actions def my_action(sender) ... end = # Interface -(IBAction) myAction:(id)sender; # Implementation -(IBAction) myAction:(id)sender { ... }
  • WAIT, WHAT ABOUT MACRUBY FOR IPHONE/IPAD?
  • MAC RUBY Xcode
  • MAC RUBY Interface Builder
  • MAC RUBY Instruments
  • MAC RUBY Let’s build something ...
  • TDD IN MACRUBY • Good supports for tests • AnyRuby testing framework instantly becomes an Objective-C testing framework • Test::Unit • RSpec • Cucumber • etc...
  • 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
  • MACRUBY VS HOTCOCOA win = NSWindow.alloc.initWithContentRect([10,20,300,300], :styleMask => (NSTitleWindowMask | NSCloseableWindowMask | NSMiniatureizableWindowMask | NSResizeableWindowMask) = win = window :frame => [10,20,300,300]
  • 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
  • MAC RUBY Very exciting future
  • MAC RUBY It’s Ruby for Cocoa Apps!
  • 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