Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to RubyMotion

Introduction to RubyMotion

Tim Riley

June 19, 2012
Tweet

More Decks by Tim Riley

Other Decks in Technology

Transcript

  1. But one thing that didn’t change was that I had

    a lot of this. And it’s the coffee drinking that’s kind of taken me to the point where I can talk to you guys today.
  2. It was what led me to help build Decaf Sucks.

    It’s a Rails app, for café reviews.
  3. And today I’ll talk about a bridge between these two

    worlds of tech: RubyMotion. So what is RubyMotion?
  4. RubyMotion And today I’ll talk about a bridge between these

    two worlds of tech: RubyMotion. So what is RubyMotion?
  5. RubyMotion is two things. First, a new implementation of Ruby

    integrated with the iOS runtime. Includes a static compiler that compiles Ruby into optimized machine code. This part’s proprietary. Second, a set of command line tools create, manage and interactively develop RubyMotion projects. Some of this has been recently open sourced.
  6. "RubyMotion" "Objective-C" [@ isEqualToString: @ ]; Let’s compare. RubyMotion builds

    upon the foundation of Objective-C, starting with the basic object model. Both Ruby and Objective-C both have a common ancestor in Smalltalk, so there's a lot of similarities: Open classes, Single inheritance, Single dynamic message dispatch
  7. "RubyMotion" "Objective-C" == Let’s compare. RubyMotion builds upon the foundation

    of Objective-C, starting with the basic object model. Both Ruby and Objective-C both have a common ancestor in Smalltalk, so there's a lot of similarities: Open classes, Single inheritance, Single dynamic message dispatch
  8. @class Foo - (id)instanceMethod; + (id)classMethod; @end One header for

    every class. We don’t need them any more. In Ruby, the class definitions are the class interfaces. In general, RubyMotion gets rid of a lot of the shitwork in Objective-C. No more repeating yourself.
  9. One header for every class. We don’t need them any

    more. In Ruby, the class definitions are the class interfaces. In general, RubyMotion gets rid of a lot of the shitwork in Objective-C. No more repeating yourself.
  10. Foundation Classes These classes are the base layer of the

    Cocoa and iOS libraries. RubyMotion translates these for you.
  11. 'hello'.uppercaseString # => 'HELLO' Building upon the Foundation classes means

    you have access to their methods. Ruby's native interface for these classes still exists, implemented on their foundation counterparts (eg. NSArray#each). This means you can use the standard Ruby interface to these basic objects, regardless of their origin.
  12. Mutable by Default eg., NSString vs NSMutableString. Be careful of

    the mutability of the objects returned by Cocoa methods.
  13. pt = CGPoint.new pt.x = 100 pt.y = 200 C

    Structures: a fixed set of labelled values in a single object. These are mapped to classes, which you can use in a number of ways, depending on your context. First, by named setter methods on an already instantiated object.
  14. error_ptr = Pointer.new(:object) C Pointers. This is one time when

    you really feel that you're outside the comfort of Ruby. A Pointer is a memory address that can point to an object. In iOS SDK, typically used as arguments to return objects by reference.
  15. notification_center.addObserverForName( name, object:object, queue:queue, usingBlock:lambda do |notification| # Handle notification

    here... end ) Should feel pretty natural to Rubyists. Use a Proc objects for the block argument.
  16. notification_center.addObserverForName( name, object:object, queue:queue, usingBlock:lambda do |notification| # Handle notification

    here... end ) Should feel pretty natural to Rubyists. Use a Proc objects for the block argument.
  17. Memory Management This is handled for you automatically. It uses

    a reference counting method, like ARC, but not exactly the same.
  18. date = NSDate.alloc.init @date = date If you want to

    ensure an object's not released, you must create a reference to it, like setting an instance variable. Or set a constant, use a class variable, add the object to a container.
  19. Enough with the theory. Let’s get some things up and

    running. Firstly, Installation: Get Xcode from the Mac App Store
  20. A Bit of UIKit Let's make a more sophisticated hello

    world, with a UIViewController custom UIView subclass.
  21. require 'motion-cocoapods' Motion::Project::App.setup do |app| # ... app.pods do dependency

    'JSONKit' end end Declare your pod dependencies in the Rakefile. You can also manually include Objective-C library dependencies using `app.vendor_project` directive. And `app.frameworks` allows control over which additional core iOS frameworks you want to use.
  22. require 'motion-cocoapods' Motion::Project::App.setup do |app| # ... app.pods do dependency

    'JSONKit' end end Declare your pod dependencies in the Rakefile. You can also manually include Objective-C library dependencies using `app.vendor_project` directive. And `app.frameworks` allows control over which additional core iOS frameworks you want to use.
  23. RubyMotion Benefits The expressiveness of Ruby (featured in the library

    examples we’ve just seen). For us, it’s also a language we already know.
  24. describe "Application 'helloworld'" do before do @app = UIApplication.sharedApplication end

    it "has one window" do @app.windows.size.should == 1 end end Ruby’s strong testing tools and culture. RSpec is built-in.
  25. You get to use your own tools. Opening up the

    toolset choices also means there’s room for innovation here. Think about all the convenience tools we have in Rails: guard, code coverage, testing tools, etc.
  26. No more .xcodeproj bullshit. The project structure is simpler and

    neater, better for collaboration. The single Rakefile expresses your intent much more clearly.
  27. No more .xcodeproj bullshit. The project structure is simpler and

    neater, better for collaboration. The single Rakefile expresses your intent much more clearly.
  28. Learning More Free screencast at The Pragmatic Studio, RubyMotion example

    apps, List of known RubyMotion projects, Cocoa books and learning resources, Big Nerd Ranch examples.