RubyMotion 買ったので、まずはお決まりから。

Hello World

まずは RubyMotion プロジェクトを生成します。

$ motion create Hello
    Create Hello
    Create Hello/.gitignore
    Create Hello/app/app_delegate.rb
    Create Hello/Gemfile
    Create Hello/Rakefile
    Create Hello/resources/Default-568h@2x.png
    Create Hello/spec/main_spec.rb

Hello ディレクトリが作成され、その中にいくつかのファイルが生成されます。

Rakefile は Rake タスクを実行するために必要なんですが、RubyMotion ではこの中にアプリケーションの設定だったり、ライブラリーのインポートだったりを記述します。

app_delegate.rb はそのまま AppDelegate です。 こんな感じ。

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    true
  end
end

さて、あとは手っ取り早く御題目を達成することにします。

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    alert = UIAlertView.new
    alert.message = "Hello Motion!"

    alert.show

    puts "Hello Motion!!"

    true
  end
end

puts を使うとてっとりばやくデバッグができます。

あとはビルドして実行します。

$ rake
     Build ./build/iPhoneSimulator-7.1-Development
   Compile ./app/app_delegate.rb
    Create ./build/iPhoneSimulator-7.1-Development/Hello.app
      Link ./build/iPhoneSimulator-7.1-Development/Hello.app/Hello
    Create ./build/iPhoneSimulator-7.1-Development/Hello.app/PkgInfo
    Create ./build/iPhoneSimulator-7.1-Development/Hello.app/Info.plist
      Copy ./resources/Default-568h@2x.png
    Create ./build/iPhoneSimulator-7.1-Development/Hello.dSYM
  Simulate ./build/iPhoneSimulator-7.1-Development/Hello.app
(main)> Hello Motion!!
(main)>

エミュレーターが起動してアプリが表示されます。

image01

今日はここまで。