mfks17's blog(Life is Good !!)

趣味や思った事を書いていくと思います

るびも事始め

こんにちは。

今日から新年度を迎えられてた方も、多いのではないでしょうか? かくいう、僕もその一人です。 新年度は何かをはじめる良い機会。僕もなにかはじめようかなと思い、このプレゼン資料にばったり遭遇してしまって、今でしょ!!っと、思ってしまいました。

ということで、るびももとい、RubyMotionをやってみようと思います。 ネットで検索してみたところ、既にコミニュティの活動が盛んで、ベルギーで初のカンファレンスが行われた様です。 ベルギーといえば、中学の卓球部時代にJ・セイブという選手に憧れていたなぁ、なんてことは置いておいて、早速はじめていきたいと思います。

上記のスライドに目を通せば、なんとなくですが概要はつかめると思います。(クオリティ高杉) ともあれ、RubyMotion購入後インストーラをゲットしてインストール。

ここからは、ありがたい日本語訳のチュートリアルを進めていきます。

とその前に、RubyMotionをはじめるに当たって大変お世話になっているリンクを貼っておきます。

Web


  • http://rubymotion.jp/

  • RubyMotionに関する情報を日本語で提供しているサイト。素晴らしい。

  • http://tutorial.rubymotion.jp/

  • こちらも本家サイトを有志の方々が日本語に翻訳してくださっています。

    Blog


  • http://blog.rubymotion.com/

  • 本家のブログ

  • Watson’s Blog

  • MacRuby,RubyMotionの中の人であるWatsonさんのブログ

  • http://satococoa.github.io/

  • RubyMotionJPの中の人satococoaさんのブログ。 なんと、お仕事でもRails & RubyMotionだそうです。

  • naoyaのはてなダイアリー

  • naoya_itoさんのブログ。いわずもがなですね。

    Dev


  • http://qiita.com/tags/RubyMotion

  • QiitaのRubyMotionに関する記事

  • http://stackoverflow.com/questions/tagged/rubymotion

  • stackoverflowのRubymotionに関する記事

  • http://www.adventar.org/calendars/18

  • 2012年のAdvent Calendarです。

  • http://www.rubymotion.com/developer-center/api/index.html

  • Rubymotionのリファレンス(英語)です。

  • https://github.com/HipByte/RubyMotionSamples

  • Hipbyteさんが公開しているサンプルリポジトリです。

  • https://developer.apple.com/jp/devcenter/ios/library/japanese.html

  • Appleさんのドキュメントです。英語の方が最新ですが、最初は日本語でもいいと思います。

    community


  • https://groups.google.com/forum/?fromgroups#!forum/rubymotion

  • RubyMotionのgoogleグループ(英語)

  • href="http://www.facebook.com/groups/149315595198329/">http://www.facebook.com/groups/149315595198329/

  • RubyMotionのフェイスブックグループ

    まだまだ、紹介仕切れないですが今回はこの辺で。随時追加していきたいと思います。

    チュートリアルをはじめる


    必要なもの

    • Xcode(Command Line Tools)
    • Rubymotion
    • お好きなエディタ

    ソフトウェアのアップデート

    $ sudo motion update
    $ motion -v
    1.35 
    

    1.35での変更点は以下の様な感じです。

    = RubyMotion 1.35 =

    Added support for string constants defined in 3rd-party header files using the macro system (ex. #define MyConstant @”SomeValue”).
    Added the no_start' option torake device debug=1’ to let the user attach a custom debugger. This is for the RubyMine integration.
    Fixed a bug in the REPL where the history could not be saved if it contained UTF-8 characters.
    Fixed a caching bug in Object#respond_to? which could return true for a method that did not exist.
    Fixed a bug in REPL where an error message was often printed after using the quit' orexit’ command.
    Fixed a bug where the AVPlayer#currentTime method caused a `{?=qiIq}@:’ not precompiled’ error.
    Improved `rake spec’ error messages to include more details. Thanks to Steven Webb for the patch (pull request #68).
    Improved rake spec files=...' to make the_spec’ suffix optional. Thanks to Dave Lee for the patch (pull request #71).
    Fixed a bug where using the AudioQueueBuffer structure would cause a crash.
    Added the :force_load' option for theapp.vendor_project’ Rakefile method. This flag has a default value of true' and can be set offalse’ when vendoring 3rd-party libraries that include duplicated symbols.
    Added the Dir#to_path method for CRuby compatibility.
    Fixed a bug where calling #alias on a new method would cause a crash.
    Fixed a bug where defining a method with #define_method on a module then messaging the method would cause a crash.
    Fixed a bug where ‘rake device debug=1’ would cause a crash if the project name included UTF-8 characters.
    Fixed a bug in the build system where framework dependencies would not properly be loaded.
    Fixed a rare bug in the `super’ dispatcher.

    だそうです。

    お馴染み”Hello World

    $ motion create Hello
        Create Hello
        Create Hello/.gitignore
        Create Hello/Rakefile
        Create Hello/app
        Create Hello/app/app_delegate.rb
        Create Hello/resources
        Create Hello/resources/Default-568h@2x.png
        Create Hello/spec
        Create Hello/spec/main_spec.rb
    $ cd Hello
    $ tree
    .
    ├── Rakefile
    ├── app
    │   └── app_delegate.rb
    ├── resources
    │   └── Default-568h@2x.png
    └── spec
        └── main_spec.rb
    

    まず、motion create puroject_nameでプロジェクトのひな形を生成します。

    Xcodeで言うところのEmpty Projectを生成するんですね。こんな感じで。

    で、違いがあるところといえば、specディリレクトリ内のファイルmain_spec.rbですかね。

    これは、RubyでいうところのRSpec形式でBDD(Behavior Driven Development)の記述をするもの(本当はBaconというRSpecのクローンみたいです。)の様です。

    さて、ここで以下のコマンドを打つと

    $ rake
    Build ./build/iPhoneSimulator-6.1-Development
       Compile ./app/app_delegate.rb
        Create ./build/iPhoneSimulator-6.1-Development/Hello.app
          Link ./build/iPhoneSimulator-6.1-Development/Hello.app/Hello
        Create ./build/iPhoneSimulator-6.1-Development/Hello.app/Info.plist
        Create ./build/iPhoneSimulator-6.1-Development/Hello.app/PkgInfo
          Copy ./resources/Default-568h@2x.png
        Create ./build/iPhoneSimulator-6.1-Development/Hello.dSYM
    warning: no debug symbols in executable (-arch i386)
      Simulate ./build/iPhoneSimulator-6.1-Development/Hello.app
    (main)>
    

    コンパイルが走り、シミュレータが立ち上がります。
    この時点では、暗い画面が表示されるのみです。
    今回は、アラートビューを表示させて見またいと思います。
    デフォルトのapp_delegate.rbは以下のようになってます。

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

    では、MobiRubyを参考にサンプルを組んでみます。

    class AppDelegate
      def application(application, didFinishLaunchingWithOptions:launchOptions)
        alert = UIAlertView.alloc.initWithTitle("Hello",
                                                message: "I am RubyMotion",
                                                delegate: self,
                                                cancelButtonTitle: "I know!",
                                                otherButtonTitles: "What'?",
                                                nil)
        alert.tag = 1
        alert.show
        alert = nil
        true
      end
    
      def alertView(alertView, clickedButtonAtIndex:buttonIndex)
        if alertView.tag == 1
          if buttonIndex == 1
            app = UIApplication.sharedApplication
            url = NSURL.URLWithString("http://rubymotion.com")
            app.openURL(url)
          elsif
            alert = UIAlertView.alloc.initWithTitle("cool",
                                                    message: "Awesome!!",
                                                    delegate: self,
                                                    cancelButtonTitle: "of course",
                                                    otherButtonTitles: nil)
            alert.show
            alert = nil
        end
      end
    end
    

    雑なコードですね。
    とりあえず動いたのでよかった。
    Objective-CだとAppdelegateクラスはUIApplicationDelegateプロトコルを持っているので
    application(application, didFinishLaunchingWithOptions:launchOptions)メソッドが使えるのだと認識していたのですが、
    そんな記述はないですね。
    ちょっと調べたところ

    rake config
    

    と、コマンドを入力すると、それらしい設定たちが出てきた。
    次回はそこら辺を調べていきたいと思います。

    最後までお読みいただきありがとうございました。