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

Differences CRuby/MacRuby/RubyMotion

Avatar for Watson Watson
July 21, 2012

Differences CRuby/MacRuby/RubyMotion

Avatar for Watson

Watson

July 21, 2012
Tweet

More Decks by Watson

Other Decks in Programming

Transcript

  1. MacRubyの構成 アプリケーション YARV LLVM 鬼車 ICU 組込みライブラリ GVL Ruby VM

    GC libauto 添付ライブラリ Foundation / ライブラリ MacOSX GCDなど
  2. MacRubyとCRubyの違い • GVLを廃止 → 各スレッドが並列動作可能 • GC → libauto •

    (RubyCocoaでRubyとMacOSXのGCの動作がコン フリクトし、クラッシュしたり・・・) • キーワード付き引数をサポート • ルートクラスObject → NSObject • Objective-Cの定数や構造体、GCDなどを扱 うために独自機能を追加
  3. キーワード付き引数 - (void)init:(char*)name withPrice:(int)price { printf("%s %d", name, price); }

    ... [[Fruit alloc] init:"apple" withPrice:150]; • Objective-C class Fruit def init(name, withPrice: price) printf("%s %d\n", name, price) end end Fruit.alloc.init("apple", withPrice:150) • MacRuby キーワード
  4. オブジェクト • ルートクラスがCRubyと異なる • CRuby/Object → MacRuby/NSObject • NSObjectはObjective-Cのルートクラス •

    全てのオブジェクトはObjective-Cオブジェク ト >> obj = Object.new => <NSObject: 0x4007e2160> >> obj.methods(true,true) => [:clear_history!, :h!, :history!, .... :"performSelectorOnMainThread:withObject:waitUntilDone:", :"performSelectorOnMainThread:withObject:waitUntilDone:modes:", :"performSelector:onThread:withObject:waitUntilDone:modes:", .... ] Objective-C メソッド 利用可能
  5. 特別なクラス継承 クラス クラスの継承関係 String String → NSMutableString → NSString →

    NSObject Array Array → NSMutableArray → NSArray → NSObject Hash Hash → NSMutableDictionary → NSDictionary → NSObject Numeric Numeric → NSNumber → NSValue → NSObject Time Time → NSDate → NSObject
  6. 特別なクラス継承 • StringオブジェクトはNSStringメソッドを 呼び出せる >> framework 'Cocoa' => true >>

    "abc".writeToFile("/tmp/test.txt", >> atomically:true, >> encoding:NSUTF8StringEncoding, >> error:nil) => true • NSStringには互換性のためRubyメソッドが 追加されている • ArrayやHashなども同様
  7. BridgeSupport ~ 準備 ~ • ヘッダファイルからメタデータを生成 $ gen_bridge_metadata -c '-I

    .' foo.h > foo.bridgesupport <?xml version='1.0'?> <!DOCTYPE signatures SYSTEM "file://localhost/System/Library/ DTDs/BridgeSupport.dtd"> <signatures version='1.0'> <struct type='{Fruits=&quot;name&quot;*&quot;price&quot;i}' name='Fruits'/> <enum name='TEST_CONST' value='42'/> <function name='init_fruits'> <arg type='^{Fruits=*i}'/> </function> </signatures> 構造体 定数 C関数
  8. 定数 • Rubyでは定数は英大文字から始まる • Objective-Cは英子文字で始める定数がある • MacRubyで英大文字に! >> framework "CoreLocation"

    => true >> p kCLLocationAccuracyBest NameError: undefined local variable or method `kCLLocationAccuracyBest' for main:TopLevel >> p KCLLocationAccuracyBest -1.0 => -1.0
  9. Pointer NSError *error; [@"abc" writeToFile: @"/tmp/test.txt" atomically: true encoding: NSUTF8StringEncoding

    error: &error]; if (error) { // Τϥʔॲཧ } error = Pointer.new(:object) "abc".writeToFile("/tmp/test.txt", atomically: true, encoding: NSUTF8StringEncoding, error: error) if error[0] # Τϥʔॲཧ end • Objective-C • MacRuby ポインタ変数
  10. 構造体 • 構造体はBoxedクラスを継承したクラスに マッピング • 構造体の型情報はBridgeSupportから取得 >> load_bridge_support_file "foo.bridgesupport" =>

    main >> struct = Fruits.new("Apple", 150) => #<Fruits name="Apple" price=150> >> Fruits.superclass => Boxed https://github.com/MacRuby/MacRuby/wiki/Boxed-Class
  11. GCD • MacOSX 10.6、iOS 4で導入 • 非同期処理やマルチスレッドな処理を手軽 に扱える • Dispatchモジュールとして追加

    • Dispatch::Queue クラス • Dispatch::Group クラス • Dispatch::Source クラス • Dispatch::Semaphore クラス
  12. 独自GC • libautoを廃止、参照カウンタ方式のGCを実装 (AppleもlibautoをMacOSX 10.8で廃止リストに入 れるらしい・・・) • Ruby実装として、はじめての参照カウンタ方式 • Objective-Cが参照カウンタでメモリ管理している

    ので、相性が良い • ベンダー static ライブラリをそのまま使える • MacRubyではObjective-Cを使ったライブラリは - fobjc-gc でコンパイルする必要がある