40
MacRuby & RubyMotion Programming OS X and iOS apps with Ruby

MacRuby & RubyMotion - Madridrb May 2012

Embed Size (px)

Citation preview

Page 1: MacRuby & RubyMotion - Madridrb May 2012

MacRuby & RubyMotionProgramming OS X and iOS apps with Ruby

Page 2: MacRuby & RubyMotion - Madridrb May 2012

HiI’m @MarkVillacampa

Page 3: MacRuby & RubyMotion - Madridrb May 2012

BlameHim

Page 4: MacRuby & RubyMotion - Madridrb May 2012

MacRuby

An implementation of the Ruby language that runs on top of the Objective-C runtime and garbage collection (libauto)

Based on Ruby 1.9

Uses LLVM and supports both ahead-of-time and just-in-time compilation

Ships with OS X 10.7 Lion!!

Page 5: MacRuby & RubyMotion - Madridrb May 2012

MacRuby

Started by Laurent Sansonetti inside Apple

Initial 0.1 release in March 2008

Latest stable release (0.10) March 2011

Nightlies are very stable (0.12)

http://macruby.org/

https://github.com/MacRuby/MacRuby

Page 6: MacRuby & RubyMotion - Madridrb May 2012

Cocoa

Apple's native object-oriented API for the Mac OS X operating system

Foundation Kit + Application Kit + Core Data frameworks

Cocoa Touch: Includes gesture recognition and different UI to use in iOS

Page 7: MacRuby & RubyMotion - Madridrb May 2012
Page 8: MacRuby & RubyMotion - Madridrb May 2012

How to installInstall XCode (Mac App Store, Free)

Install Command Line Tools (Preferences > Downloads)

Install MacRuby Nightly

If you are in OS X 10.6.8, install Bridge Support Preview 3

https://github.com/MacRuby/MacRuby/wiki/Setting-up-MacRuby

Page 9: MacRuby & RubyMotion - Madridrb May 2012

Key concepts:MacRuby objects are Objective-C objects

>> "Hello Madridrb!".class.ancestors=> [String, NSMutableString, NSString, Comparable, NSObject, Kernel]

Can use Ruby & Cocoa methods

>> "Hello Madridrb!".upcase <- Ruby=> "HELLO MADRIDRB!">> "Hello Madridrb!".uppercaseString <- Cocoa=> "HELLO MADRIDRB!"

Page 10: MacRuby & RubyMotion - Madridrb May 2012

Key concepts:

New method syntax: named parameters

Cocoa classes need to be allocated and initialized

>> NSString.alloc.initWithString(“Hello Madridrb!”)=> "Hello Madridrb!"

>> NSDictionary.alloc.initWithObjects(["foo"], forKeys: ["bar"])=> {"foo"=>"bar"}

Page 11: MacRuby & RubyMotion - Madridrb May 2012

1. The syntax

Page 12: MacRuby & RubyMotion - Madridrb May 2012

Objective-C:

Ruby:

a = {"foo" => ["bar", "baz"], "oof" => 2}

NSMutableDictionary *dict = [NSMutableDictionary dictionary];[dict setObject:[NSArray arrayWithObjects:@"bar", @"baz"] forKey:@"foo"];[dict setObject:[NSNumber numberWithInt:2] forKey:@"oof"];

Page 13: MacRuby & RubyMotion - Madridrb May 2012

Objective-C:

Ruby:

a = {"foo" => ["bar", "baz"], "oof" => 2}

NSMutableDictionary *dict = [NSMutableDictionary dictionary];[dict setObject:[NSArray arrayWithObjects:@"bar", @"baz"] forKey:@"foo"];[dict setObject:[NSNumber numberWithInt:2] forKey:@"oof"];

Page 14: MacRuby & RubyMotion - Madridrb May 2012

2. XCode

Page 15: MacRuby & RubyMotion - Madridrb May 2012
Page 16: MacRuby & RubyMotion - Madridrb May 2012

Interface Builder

Page 17: MacRuby & RubyMotion - Madridrb May 2012

3. REPL Console( Read Eva l P r i n t Loop )

Page 18: MacRuby & RubyMotion - Madridrb May 2012
Page 19: MacRuby & RubyMotion - Madridrb May 2012

4. Gems

Page 20: MacRuby & RubyMotion - Madridrb May 2012

Any gem that works with Ruby 1.9 should work with MacRuby

Page 21: MacRuby & RubyMotion - Madridrb May 2012

5. Behaviour-Driven Development

Page 22: MacRuby & RubyMotion - Madridrb May 2012

A small RSpec clone, with NSRunLoop powers.

https://github.com/alloy/MacBacon

MacBacon

Page 23: MacRuby & RubyMotion - Madridrb May 2012

6. Mac App Store

Page 24: MacRuby & RubyMotion - Madridrb May 2012

http://briquetteapp.com/

Page 25: MacRuby & RubyMotion - Madridrb May 2012

http://shop.oreilly.com/product/0636920000723.do http://manning.com/lim/

Page 26: MacRuby & RubyMotion - Madridrb May 2012

RubyMotion

Built on top of MacRuby

Uses a new LLVM-based static compiler that generates optimized machine code

Memory model similar to Objective-C ARC

Based on Rake

http://www.rubymotion.com/

Page 27: MacRuby & RubyMotion - Madridrb May 2012

RubyMotion

Commercial product. Educational licenses available at discounted price.

Laurent Sansonetti left Apple to work on it.

Released May 3rd, 2012

Fast growing community!

Page 28: MacRuby & RubyMotion - Madridrb May 2012
Page 29: MacRuby & RubyMotion - Madridrb May 2012

HelloMadridrb

app

app_delegate.rb

resources

spec

main_spec.rb

.gitignore

Rakefile

$ motion create HelloMadridrb

.rb files

Main delegate

Images, Sounds, .xib files

Tests

Configuration file

Page 30: MacRuby & RubyMotion - Madridrb May 2012

1. Interactive Console

Page 31: MacRuby & RubyMotion - Madridrb May 2012

Holding and clicking in a UI element assigns that element to the self variable in the console

Page 32: MacRuby & RubyMotion - Madridrb May 2012

2. CocoaPods

Page 33: MacRuby & RubyMotion - Madridrb May 2012

CocoaPods is like RubyGems but for Ojective-C projects.

$ sudo gem install cocoapods$ pod setup$ sudo gem install motion-cocoapods

Choose a pod:https://github.com/CocoaPods/Specs

Edit Rakefile:

require 'motion-cocoapods'

Motion::Project::App.setup do |app| # ... app.pods do dependency 'JSONKit' endend

Page 34: MacRuby & RubyMotion - Madridrb May 2012

3. TestFlight

Page 35: MacRuby & RubyMotion - Madridrb May 2012

TestFlight helps you distribute development builds of your app to your betatesters.

$ sudo gem install motion-testflight

Download TestFlight’s SDK and put it in the “vendor” directory inside your app.

require 'motion-testflight'

Motion::Project::App.setup do |app| # ... app.testflight.sdk = 'vendor/TestFlight' app.testflight.api_token = '<API token>' app.testflight.team_token = '<team token>'end

http://testflightapp.com/sdk/download

Edit Rakefile:

$ rake testflight notes='First release!'

Run:

Page 36: MacRuby & RubyMotion - Madridrb May 2012

4. App Store

Page 37: MacRuby & RubyMotion - Madridrb May 2012

http://itunes.apple.com/us/app/mustachio/id525324802

https://github.com/HipByte/Mustachio

Mustachio

Page 38: MacRuby & RubyMotion - Madridrb May 2012

Demo time

Page 39: MacRuby & RubyMotion - Madridrb May 2012

Questions?

Page 40: MacRuby & RubyMotion - Madridrb May 2012

No, it doesn’t run Rails.