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

Using RubyMotion to Build Something Useful

Ray Hightower
November 19, 2013

Using RubyMotion to Build Something Useful

RubyMotion enables developers to write native iOS apps using the Ruby language. So what? Why can't we just continue writing apps in Objective-C, the Apple way?

Maybe RubyMotion is "the other Apple way" because it is the brainchild of Laurent Sansonetti, creator of MacRuby and a former Apple engineer. Both RubyMotion and Objective-C use LLVM-based tools to compile source files into native code that runs on Apple hardware. There is no performance difference between apps developed in Objective-C and those built with RubyMotion.

However, the two platforms rely on different developer tools. This talk will demonstrate some of the more popular RubyMotion libraries/gems while highlighting a few of the "gotchas". The first eight minutes will introduce RubyMotion for those who are unfamiliar with it. The remainder of the talk will feature demos of RubyMotion in action. To get the most out of the talk, the audience member should have some experience with Objective-C, Ruby, or both.

Ray Hightower

November 19, 2013
Tweet

More Decks by Ray Hightower

Other Decks in Technology

Transcript

  1. 126 def start_game_timer 127 @game_timer = NSTimer.scheduledTimerWithTimeInterval(0.01, 128 target: self,

    129 selector: "move_ball", 130 userInfo: nil, 131 repeats: true) 132 end
  2. 78 def move_ball 79 # If ball exits right, +1

    for the left player 80 if (@ball_view.center.x > self.view.frame.size.width) 81 increment_left_score 82 @direction_x *= -1 83 self.reset_ball 84 end 85 86 # If ball exits left, +1 for the right player 87 if (@ball_view.center.x < 0) 88 increment_right_score 89 @direction_x *= -1 90 self.reset_ball 91 end 92 93 # If ball hits top or bottom wall, bounce y in the opposite direction. x-direction remains unchanged. 94 if ((@ball_view.center.y + @ball_view.frame.size.height > ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! self.view.frame.size.height) || @ball_view.frame.origin.y < 0) 95 @direction_y *= -1 96 end 97 98 # If the ball didn't hit anything, keep on moving... 99 @ball_view.center = [@ball_view.center.x + @direction_x, @ball_view.center.y + @direction_y] 100 101 # If ball collides with a paddle, change direction. 102 check_paddle_collision 103 104 end
  3. def ejectThreeDisks alert = NSAlert.alloc.init response = %x(/usr/sbin/diskutil eject SiiGBlack)

    + "\n" response += %x(/usr/sbin/diskutil eject Ultra3TB) + "\n" response += %x(/usr/sbin/diskutil eject WDSilver) + "\n" alert.setMessageText response alert.addButtonWithTitle "OK" alert.runModal ! !end
  4. ~/Code/Ruby/RubyMotion$ motion create helloruby Create helloruby Create helloruby/.gitignore Create helloruby/Rakefile

    Create helloruby/app Create helloruby/app/app_delegate.rb Create helloruby/resources Create helloruby/spec Create helloruby/spec/main_spec.rb ~/Code/Ruby/RubyMotion$
  5. ~/Code/Ruby/RubyMotion$ cd helloruby/ ~/Code/Ruby/RubyMotion/helloruby$ tree . !"" Rakefile !"" app

    # $"" app_delegate.rb !"" resources $"" spec $"" main_spec.rb 3 directories, 3 files ~/Code/Ruby/RubyMotion/helloruby$
  6. ~/Code/Ruby/RubyMotion$ cd helloruby/ ~/Code/Ruby/RubyMotion/helloruby$ tree . !"" Rakefile !"" app

    # $"" app_delegate.rb !"" resources $"" spec $"" main_spec.rb 3 directories, 3 files ~/Code/Ruby/RubyMotion/helloruby$
  7. 1 class AppDelegate 2 def application(application, didFinishLaunchingWithOptions:launchOptions) 3 @window =

    UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) 4 @window.rootViewController = HomeController.new 5 @window.makeKeyAndVisible 6 @window.backgroundColor = UIColor.grayColor 7 true 8 end 9 end
  8. 1 class AppDelegate 2 def application(application, didFinishLaunchingWithOptions:launchOptions) 3 @window =

    UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) 4 @window.rootViewController = HomeController.new 5 @window.makeKeyAndVisible 6 @window.backgroundColor = UIColor.grayColor 7 true 8 end 9 end
  9. 1 class AppDelegate 2 def application(application, didFinishLaunchingWithOptions:launchOptions) 3 @window =

    UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) 4 @window.rootViewController = HomeController.new 5 @window.makeKeyAndVisible 6 @window.backgroundColor = UIColor.grayColor 7 true 8 end 9 end
  10. 1 class AppDelegate 2 def application(application, didFinishLaunchingWithOptions:launchOptions) 3 @window =

    UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) 4 @window.rootViewController = HomeController.new 5 @window.makeKeyAndVisible 6 @window.backgroundColor = UIColor.grayColor 7 true 8 end 9 end
  11. 1 class AppDelegate 2 def application(application, didFinishLaunchingWithOptions:launchOptions) 3 @window =

    UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) 4 @window.rootViewController = HomeController.new 5 @window.makeKeyAndVisible 6 @window.backgroundColor = UIColor.grayColor 7 true 8 end 9 end
  12. ~/Code/Ruby/RubyMotion$ cd helloruby/ ~/Code/Ruby/RubyMotion/helloruby$ tree . !"" Rakefile !"" app

    # $"" app_delegate.rb !"" resources $"" spec $"" main_spec.rb 3 directories, 3 files ~/Code/Ruby/RubyMotion/helloruby$
  13. 1 class FizzBuzzViewController < UIViewController 2 3 def loadView 4

    views = NSBundle.mainBundle.loadNibNamed "fbib", owner:self, options:nil 5 self.view = views[0] 6 @counter = 0 7 @view_handle = self.view 8 end
  14. 10 def viewDidLoad 11 @label = view.viewWithTag 1 12 plus_button

    = view.viewWithTag 2 13 minus_button = view.viewWithTag 3 14 reset_button = view.viewWithTag 4 15 end
  15. ~/Code/Ruby/RubyMotion/fizzbuzzrm[master]$ tree . !"" Gemfile !"" Gemfile.lock !"" Guardfile !""

    Rakefile !"" app # !"" app.rb # !"" app_delegate.rb # !"" fizzbuzz_view_controller.rb # !"" home_controller.rb # !"" kernel.rb # !"" rm-ansiterm.rb # $"" string.rb !"" interfaces !"" resources $"" spec !"" 00-redgreen.rb !"" home_controller_spec.rb $"" main_spec.rb
  16. 1 describe "FizzBuzz Calculator Function" do 2 before do 3

    @this_number = 38 4 end ... 17 18 (1..10).each do |this_number| 19 if this_number % 15 == 0 20 it 'should return buzz for multiples of 15' do 21 HomeController.fizzbuzz_calc(this_number).should == 'fizzbuzz' 22 end 23 elsif this_number % 3 == 0 24 it 'should return fizz for multiples of 3' do 25 HomeController.fizzbuzz_calc(this_number).should == 'fizz' 26 end 27 elsif this_number % 5 == 0 28 it 'should return buzz for multiples of 5' do 29 HomeController.fizzbuzz_calc(this_number).should == 'buzz' 30 end 31 else 32 it 'should return the original number' do 33 HomeController.fizzbuzz_calc(this_number).should == this_number 34 end 35 end 36 end 37 end home_controller_spec.rb
  17. describe "FizzBuzz Calculator Function" do before do @this_number = 38

    end ... (1..10).each do |this_number| if this_number % 15 == 0 it 'should return buzz for mults of 15' do HomeController.fizzbuzz_calc(this_number) .should == 'fizzbuzz' end home_controller_spec.rb
  18. 1 $:.unshift("/Library/RubyMotion/lib") 2 require 'motion/project/template/ios' 3 require "bundler/gem_tasks" 4 require

    "bundler/setup" 5 6 $:.unshift("./lib/") 7 require './lib/formotion' 8 9 Motion::Project::App.setup do |app| 10 # Use `rake config' for project settings. 11 app.name = 'Formotion' 12 end Rakefile Source: http://clayallsopp.com
  19. 1 class LoginController < Formotion::FormController 2 def init 3 form

    = Formotion::Form.new({ 4 title: "Login", 5 sections: [{ 6 rows: [{ 7 title: "User Name", 8 type: :string, 9 placeholder: "name", 10 key: :user 11 }, { 12 ... Source: http://clayallsopp.com
  20. ~/Code/Ruby/RubyMotion$ ios profiles:list +----------------------------------+--------------+---------+ | Profile | App ID |

    Status | +----------------------------------+--------------+---------+ | WisdomGroup iOS Development | ABCDEFGHIJ.* | Active | | WisdomGroup RubyMotion | ABCDEFGHIJ.* | Active | | ChicagoRubyRubyMotion iPad | ABCDEFGHIJ.* | Active | | iOS Team Provisioning Profile: * | ABCDEFGHIJ.* | Active | | RTHiPhone4S | ABCDEFGHIJ.* | Active | +----------------------------------+--------------+---------+ ~/Code/Ruby/RubyMotion$ ios help $ gem install cupertino
  21. Resources http://rubymotion-tutorial.com Multi-page tutorial by Clay Allsopp. Also see http://clayallsopp.com

    http://pragmaticstudio.com/screencasts/rubymotion Pragmatic Studio’s intro to RubyMotion (free) plus a paid intro to Ruby. http://chicagoruby.org/videos/archives/2012/09/04/rubymotion-for-rubyists/ Bill Svec builds a simple RubyMotion weather app in about 90 minutes. http://motioncasts.tv Tutorial videos by Michael Erasmus and Hendrik Swanepoel. http://nshipster.com Mattt Thompson's weekly journal of Objective-C and Cocoa. http://nsscreencast.com/ Ben Scheirman’s Objective-C screencasts, plus a short one on RubyMotion.