Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
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
35
RMagick, migrate to ImageMagick 7 #RubyKaigi #RubyKaigi2019
watson
0
10k
Magick
watson
0
12k
Improve JSON Performance
watson
2
1.1k
fastlane 奮闘記
watson
0
99
How to optimize Ruby internal.
watson
3
3.1k
iOS、AndroidアプリをRubyで
watson
1
520
RubyMotion 2.0
watson
6
2.9k
Other Decks in Programming
See All in Programming
Webの地図
yosuke_furukawa
PRO
6
4k
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
7
2.7k
App Intentsのビルドプロセスを支える技術
kntkymt
0
200
世界の中心で、AI(App Intents)をさけぶ ー App Intents中心設計の実践ガイド
touyou
0
340
LLMは4年分のCompose移行を再現できるのか?実プロダクト279件のXMLで探る自動化の境界線
makun
0
520
20260828_品質と開発生産性を両立させる、AI時代のE2Eテストの考え方
magicpod
0
180
typoなんかねぇよ
raspython3
0
680
新卒PdEのリアル
ryu1013
1
480
Laravelのアプリケーションをどこにデプロイするか #ツナギメオフライン.9
akase244
0
120
ソフトウェアラスタライザ
fadis
1
800
デプロイ直後のレイテンシスパイクを調べたら、 Railsの仕様にたどり着いた
nhsykym
0
110
Gmail/Google DriveをトリガーにAIエージェントを動かそう! / Run AI agents with Gmail/Google Drive as triggers!
har1101
3
490
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.9k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
880
Color Theory Basics | Prateek | Gurzu
gurzu
0
460
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
330
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
270
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
490
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
300
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
4
620
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.2k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.4k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
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
ご清聴 ありがとうございました