Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Differences CRuby/MacRuby/RubyMotion
Search
Watson
July 21, 2012
Programming
5.8k
5
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Differences CRuby/MacRuby/RubyMotion
Watson
July 21, 2012
More Decks by Watson
See All by Watson
クラウドネイティブ時代に 進化し続けるFluentd
watson
0
30
RMagick, migrate to ImageMagick 7 #RubyKaigi #RubyKaigi2019
watson
0
10k
Magick
watson
0
12k
Improve JSON Performance
watson
2
1.1k
fastlane 奮闘記
watson
0
98
How to optimize Ruby internal.
watson
3
3.1k
iOS、AndroidアプリをRubyで
watson
1
510
RubyMotion 2.0
watson
6
2.9k
Other Decks in Programming
See All in Programming
いまどきの Codex で開発する visionOS アプリの開発スタイルについて
karad
0
170
React本体のコードリーディング
high_g_engineer
1
160
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
570
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
440
Pythonの実行はどこまで賢くなったのか? CPythonとPyPyから見る最適化のしくみ
curekoshimizu
4
2.3k
プロポーザルを書いてもらう
pvcresin
0
590
AI時代に学ぶ 好きなルール 嫌いなルール Linter編
shorty5121
0
220
楽しそうなつよつよエンジニアと目が死んでる僕/A brilliant engineer having a blast, and dead-eyed me.
3l4l5
2
260
仕様駆動開発の消費期限
watany
20
9.1k
高専キャリア LT 発表内容
crysta1221
5
4.8k
Deep dive into the select statement (GopherCon UK)
jespino
0
160
[PyCon KR 2026] More Variants, More Diversity for AI Accelerators
achimnol
0
120
Featured
See All Featured
We Are The Robots
honzajavorek
0
320
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
280
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.1k
Claude Code のすすめ
schroneko
67
230k
Building Applications with DynamoDB
mza
96
7.2k
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.1k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
490
エンジニアに許された特別な時間の終わり
watany
108
250k
The Spectacular Lies of Maps
axbom
PRO
1
950
Crafting Experiences
bethany
1
270
Transcript
Differences CRuby/MacRuby/RubyMotion twitter: @watson1978
自己紹介 • MacRubyのコミッターしています • コミットが1000回を超えました!
アジェンダ • MacRuby/RubyMotionの系譜 • CRuby vs MacRuby • MacRuby vs
RubyMotion
MacRuby RubyMotionの系譜
系譜 CRuby MacRuby RubyMotion 2008/02 2011/11 1.9.0 p1 2008/03 v0.11
2011/11 v1.0 2012/05
CRuby vs MacRuby
CRuby1.9の構成 アプリケーション YARV 鬼車 組込みライブラリ GVL Ruby VM GC 添付ライブラリ
ライブラリ OS
MacRubyの構成 アプリケーション YARV LLVM 鬼車 ICU 組込みライブラリ GVL Ruby VM
GC libauto 添付ライブラリ Foundation / ライブラリ MacOSX GCDなど
MacRubyとCRubyの違い • GVLを廃止 → 各スレッドが並列動作可能 • GC → libauto •
(RubyCocoaでRubyとMacOSXのGCの動作がコン フリクトし、クラッシュしたり・・・) • キーワード付き引数をサポート • ルートクラスObject → NSObject • Objective-Cの定数や構造体、GCDなどを扱 うために独自機能を追加
キーワード付き引数 - (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 キーワード
オブジェクト • ルートクラスが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 メソッド 利用可能
特別なクラス継承 クラス クラスの継承関係 String String → NSMutableString → NSString →
NSObject Array Array → NSMutableArray → NSArray → NSObject Hash Hash → NSMutableDictionary → NSDictionary → NSObject Numeric Numeric → NSNumber → NSValue → NSObject Time Time → NSDate → NSObject
特別なクラス継承 • StringオブジェクトはNSStringメソッドを 呼び出せる >> framework 'Cocoa' => true >>
"abc".writeToFile("/tmp/test.txt", >> atomically:true, >> encoding:NSUTF8StringEncoding, >> error:nil) => true • NSStringには互換性のためRubyメソッドが 追加されている • ArrayやHashなども同様
注意点 • NSStringクラスのメソッドが返す文字列はNSString • Stringクラスに追加したメソッドは、NSStringのオブ ジェクトから利用できない(継承関係を確認しよう!) • Stringクラス/NSStringクラスのメソッドが返す文字 列の両方で同じメソッドを使いたい場合には、 NSStringにメソッドを追加すると幸せに。
• NSMutableStringやNSArray、NSDictionaryでも同様 >> str = NSString.stringWithString("abc") => "abc" >> str.class => String #=> 実際はNSStringオブジェクト
BridgeSupport • MacRubyはObjective-C Runtimeを使用し てメソッド追加やインスタンス変数など扱う • Objective-C Runtime クラスにどのようなメソッドが実装されてい るか、そのメソッドの引数が幾つあるのか等
定数値、構造体の型、C関数インタフェース、 可変長引数やBlocksを扱うメソッド → BridgeSupportで解決
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="name"*"price"i}' name='Fruits'/> <enum name='TEST_CONST' value='42'/> <function name='init_fruits'> <arg type='^{Fruits=*i}'/> </function> </signatures> 構造体 定数 C関数
BridgeSupport ~ 利用 ~ • frameworkにBridgeSupportファイルがあれば、 frameworkと一緒にロードされる(OSX標準の frameworkにはあらかじめ付属) • 手動でBridgeSupportファイルをロードもできる
framework "CoreLocation" load_bridge_support_file "foo.bridgesupport" https://github.com/MacRuby/MacRuby/wiki/Loading- Objective-C-Frameworks-and-Bundles
定数 • 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
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 ポインタ変数
Pointer • ポインタ変数が必要なメソッド呼び出しで 使用する • void型のポインタなど扱えないので注意 • ポインタ変数をできるだけ使わないようにライブ ラリを作っていく方が幸せかも・・・ https://github.com/MacRuby/MacRuby/wiki/Pointer-Class
構造体 • 構造体は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
GCD • MacOSX 10.6、iOS 4で導入 • 非同期処理やマルチスレッドな処理を手軽 に扱える • Dispatchモジュールとして追加
• Dispatch::Queue クラス • Dispatch::Group クラス • Dispatch::Source クラス • Dispatch::Semaphore クラス
GCD • 並列処理を手軽に実装可能 • RubyGemsのDispatchを使うと、timesや each、mapで並列処理可能に Dispatch::Queue.concurrent do # 並列処理
end ary.p_each do # 並列処理 end https://github.com/MacRuby/MacRuby/wiki/Dispatch- Module
MacRuby vs RubyMotion
MacRubyの構成 アプリケーション LLVM ICU 組込みライブラリ Ruby VM libauto 添付ライブラリ Foundation
/ ライブラリ MacOSX GCDなど
RubyMotionの構成 アプリケーション LLVM ICU 組込みライブラリ Ruby VM libauto 独自GC 添付ライブラリ
Foundation / ライブラリ iOS GCDなど
MacRubyとRubyMotionの 違い • Rubyソースをネイティブの実行ファイルに コンパイル • 添付ライブラリは廃止 (Kernel.#requireメ ソッドも動かない) •
GCを独自に実装
コンパイルのフロー Rubyソース LLVM中間コード アセンブリファイル オブジェクトファイル 実行ファイル 組込みライブラリ iOS Framework ベンダー
static ライブラリ アプリ
独自GC • libautoを廃止、参照カウンタ方式のGCを実装 (AppleもlibautoをMacOSX 10.8で廃止リストに入 れるらしい・・・) • Ruby実装として、はじめての参照カウンタ方式 • Objective-Cが参照カウンタでメモリ管理している
ので、相性が良い • ベンダー static ライブラリをそのまま使える • MacRubyではObjective-Cを使ったライブラリは - fobjc-gc でコンパイルする必要がある
独自GC • オブジェクトの生存期間が、NSAutoReleasePoolが設 定されている区間に依存している • イベントループの区間を越えてオブジェクトを利用する場 合には、インスタンス変数などに格納する • 参照カウンタ方式のため循環参照でメモリリークする ので注意が必要
• メモリ管理を新しくするらしいので、経過を見守りま しょう!! ary = [1, 2, "foo", "bar"] 100_000_000.times do ary << ary end
ご清聴 ありがとうございました