【RBC Tech Saturday】
MobiRubyでiOSアプリをつくろう
∼スマホアプリ開発にmrubyがやってきた∼

            2012/9/22




        村部 淳也

      http://www.lancard.com/
自己紹介

• 村部 淳也
  • アクセス
    • Twitter:@murave
    • Mail:murabe@lancard.com

 • 所属コミュニティ
   • Rubyビジネス・コモンズ(スタッフ)
   • 他、いろんなところに出没
すけじゅーる
• 14:00 MobiRuby
 •       現在の状況(MobiRuby作者、増井さんの資料で紹介)
     •     mruby入門(九州工業大学、田中先生の資料で紹介)
 •       コードを読み書き
     •     サンプル書いててハマったところなど

• 16:00 その他Ruby系スマホ開発
 •       RubyMotion(RBC最首会長の資料で紹介)
 •       Rhodes (& Phonegap) (RBC古川さん)



• 懇親会
 •       オクトーバーフェストでビール!ビール!
C関数の呼び出し
• CFunc::call(返り値の型, 関数名, 引数...)


• C = CFunc されてるので C::call でもOK
 •   mobiruby.rb にあります。


• 型はCFuncのクラスメソッドで
 •   cfunc_rb.rb参照。
 •   C::SInt32、C::Pointerなど
 •   C::Voidも忘れずに
構造体の扱い
class TestStruct < CFunc::Struct
  define CFunc::SInt8, :x,
         CFunc::SInt16, :y
end


test = TestStruct.new
test[:x] = 10
Cocoaブリッジ
• Objective-Cのメソッドは頭に_つけて呼ぶ

        screen_rect = Cocoa::UIScreen._mainScreen._bounds
        view = Cocoa::UIView._alloc._initWithFrame(screen_rect)


• Objective-Cのメソッド定義はdefineで
    •     define は cocoa.rb で定義されてます
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        //処理
}



define C::Void, :alertView, C::Pointer, :clickedButtonAtIndex, C::SInt32 do ¦me, index¦
        #処理
end
Cocoaブリッジ
• 構造体の扱い

 point = Cocoa::Struct::CGPoint.new
 point[:x], point[:y] = x.to_f, y.to_f
requireでハマる
• コンパイルされてないとだめです
BuildPhasesのCompile Source
Build Rules
C言語化されたバイトコード
 • app.rb → app.c
const char mruby_data_app[] = {
0x52,0x49,0x54,0x45,0x30,0x30,0x30,0x39,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x39,
0x30,0x30,0x30,0x30,0x4d,0x41,0x54,0x5a,0x20,0x20,0x20,0x20,0x30,0x30,0x30,0x39,
0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x93,0x00,0x01,0x00,0x00,0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0xe6,0xe5,0x00,0x00,0x00,0x55,0x53,0x43,0x00,0x02,0x00,0x04,
0x00,0x02,0x6f,0x28,0x00,0x00,0x00,0x07,0x01,0x00,0x00,0x06,0x01,0x80,0x00,0x3d,
0x01,0x00,0x00,0xa0,0x01,0x00,0x00,0x06,0x01,0x80,0x00,0xbd,0x01,0x00,0x00,0xa0,
0x00,0x00,0x00,0x4a,0x06,0xf8,0x00,0x00,0x00,0x02,0x10,0x00,0x08,0x6d,0x6f,0x62,
0x69,0x72,0x75,0x62,0x79,0x10,0x00,0x06,0x6d,0x75,0x72,0x61,0x76,0x65,0x8d,0x31,
0x00,0x00,0x00,0x01,0x00,0x07,0x72,0x65,0x71,0x75,0x69,0x72,0x65,0xa4,0x7e,0x00,
0x00,0x00,0x00,
};
defineでハマる
• Cocoaのメソッド定義に使用
•      型を指定する必要があるため
•      cocoa.rbで実装されてます



• 適切な型を指定しないとあとで困ったり
•      例えば、hello.rb内の次の定義
    define C::Void, :alertView, C::Pointer, :clickedButtonAtIndex, C::SInt32 do ¦me, index¦

•      meにC::Pointer型でイベント対象がセットされる
•      Objective-Cのオブジェクトのidはポインタなので間違いではない
•      しかし me._tag 等メソッドを呼ぶとエラーになる
•      適切な型を指定すればOK
    define C::Void, :alertView, Cocoa::UIAlertView, :clickedButtonAtIndex, C::SInt32 do ¦me,
    index¦
関係資料
•   増井さんRubykaigiスライド
    http://www.scribd.com/doc/105622019/MobiRuby-introduction

•   増井さんRubykaigiビデオ
    http://www.ustream.tv/recorded/25396312
    http://www.ustream.tv/recorded/25396851
•   muraveサンプルプログラム

    http://www.lancard.com/blog/2012/09/21/mobirubyのサンプル作りまし
    た動画付/

•   RBC最首会長RubyMotionスライド

    http://www.slideshare.net/saishu/ruby-motion-20127
•   RBC古川さんRhodesスライド
    http://www.slideshare.net/purukyawa/iphoneandroid-13906013

MobiRubyでiOSアプリをつくろう

  • 1.
  • 2.
    自己紹介 • 村部 淳也 • アクセス • Twitter:@murave • Mail:murabe@lancard.com • 所属コミュニティ • Rubyビジネス・コモンズ(スタッフ) • 他、いろんなところに出没
  • 3.
    すけじゅーる • 14:00 MobiRuby • 現在の状況(MobiRuby作者、増井さんの資料で紹介) • mruby入門(九州工業大学、田中先生の資料で紹介) • コードを読み書き • サンプル書いててハマったところなど • 16:00 その他Ruby系スマホ開発 • RubyMotion(RBC最首会長の資料で紹介) • Rhodes (& Phonegap) (RBC古川さん) • 懇親会 • オクトーバーフェストでビール!ビール!
  • 4.
    C関数の呼び出し • CFunc::call(返り値の型, 関数名,引数...) • C = CFunc されてるので C::call でもOK • mobiruby.rb にあります。 • 型はCFuncのクラスメソッドで • cfunc_rb.rb参照。 • C::SInt32、C::Pointerなど • C::Voidも忘れずに
  • 5.
    構造体の扱い class TestStruct <CFunc::Struct define CFunc::SInt8, :x, CFunc::SInt16, :y end test = TestStruct.new test[:x] = 10
  • 6.
    Cocoaブリッジ • Objective-Cのメソッドは頭に_つけて呼ぶ screen_rect = Cocoa::UIScreen._mainScreen._bounds view = Cocoa::UIView._alloc._initWithFrame(screen_rect) • Objective-Cのメソッド定義はdefineで • define は cocoa.rb で定義されてます - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { //処理 } define C::Void, :alertView, C::Pointer, :clickedButtonAtIndex, C::SInt32 do ¦me, index¦ #処理 end
  • 7.
    Cocoaブリッジ • 構造体の扱い point= Cocoa::Struct::CGPoint.new point[:x], point[:y] = x.to_f, y.to_f
  • 8.
  • 9.
  • 10.
  • 11.
    C言語化されたバイトコード • app.rb→ app.c const char mruby_data_app[] = { 0x52,0x49,0x54,0x45,0x30,0x30,0x30,0x39,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x39, 0x30,0x30,0x30,0x30,0x4d,0x41,0x54,0x5a,0x20,0x20,0x20,0x20,0x30,0x30,0x30,0x39, 0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x93,0x00,0x01,0x00,0x00,0x20,0x20,0x20,0x20, 0x20,0x20,0x20,0x20,0xe6,0xe5,0x00,0x00,0x00,0x55,0x53,0x43,0x00,0x02,0x00,0x04, 0x00,0x02,0x6f,0x28,0x00,0x00,0x00,0x07,0x01,0x00,0x00,0x06,0x01,0x80,0x00,0x3d, 0x01,0x00,0x00,0xa0,0x01,0x00,0x00,0x06,0x01,0x80,0x00,0xbd,0x01,0x00,0x00,0xa0, 0x00,0x00,0x00,0x4a,0x06,0xf8,0x00,0x00,0x00,0x02,0x10,0x00,0x08,0x6d,0x6f,0x62, 0x69,0x72,0x75,0x62,0x79,0x10,0x00,0x06,0x6d,0x75,0x72,0x61,0x76,0x65,0x8d,0x31, 0x00,0x00,0x00,0x01,0x00,0x07,0x72,0x65,0x71,0x75,0x69,0x72,0x65,0xa4,0x7e,0x00, 0x00,0x00,0x00, };
  • 12.
    defineでハマる • Cocoaのメソッド定義に使用 • 型を指定する必要があるため • cocoa.rbで実装されてます • 適切な型を指定しないとあとで困ったり • 例えば、hello.rb内の次の定義 define C::Void, :alertView, C::Pointer, :clickedButtonAtIndex, C::SInt32 do ¦me, index¦ • meにC::Pointer型でイベント対象がセットされる • Objective-Cのオブジェクトのidはポインタなので間違いではない • しかし me._tag 等メソッドを呼ぶとエラーになる • 適切な型を指定すればOK define C::Void, :alertView, Cocoa::UIAlertView, :clickedButtonAtIndex, C::SInt32 do ¦me, index¦
  • 13.
    関係資料 • 増井さんRubykaigiスライド http://www.scribd.com/doc/105622019/MobiRuby-introduction • 増井さんRubykaigiビデオ http://www.ustream.tv/recorded/25396312 http://www.ustream.tv/recorded/25396851 • muraveサンプルプログラム http://www.lancard.com/blog/2012/09/21/mobirubyのサンプル作りまし た動画付/ • RBC最首会長RubyMotionスライド http://www.slideshare.net/saishu/ruby-motion-20127 • RBC古川さんRhodesスライド http://www.slideshare.net/purukyawa/iphoneandroid-13906013