ruby/rubycocoa for iphone - uicoderz4

149
Ruby / RubyCocoa for iPhone @takuma104

Upload: takuma-mori

Post on 03-Dec-2014

7.794 views

Category:

Documents


1 download

DESCRIPTION

this slide is written in Japanese iphone-rubycocoa project (in English): http://wiki.github.com/takuma104/iphone-rubycocoa

TRANSCRIPT

Page 1: Ruby/RubyCocoa for iPhone - UICoderz4

Ruby / RubyCocoafor iPhone

@takuma104

Page 2: Ruby/RubyCocoa for iPhone - UICoderz4

発端

Page 3: Ruby/RubyCocoa for iPhone - UICoderz4

iPhoneでC#(CIL)が動いてる

Page 4: Ruby/RubyCocoa for iPhone - UICoderz4

monoでAOTで動くらしい

Page 5: Ruby/RubyCocoa for iPhone - UICoderz4

じゃ

Page 6: Ruby/RubyCocoa for iPhone - UICoderz4

Rubyも(もちろん)動くよね??

Page 7: Ruby/RubyCocoa for iPhone - UICoderz4

では

Page 8: Ruby/RubyCocoa for iPhone - UICoderz4

移植

Page 9: Ruby/RubyCocoa for iPhone - UICoderz4

というかなんとか動かしたい

Page 10: Ruby/RubyCocoa for iPhone - UICoderz4

まずRubyの話

Page 11: Ruby/RubyCocoa for iPhone - UICoderz4

話はOS1.0.x時代 (JB)

Page 12: Ruby/RubyCocoa for iPhone - UICoderz4

Rubyを動かすのが流行った

Page 13: Ruby/RubyCocoa for iPhone - UICoderz4

Page 14: Ruby/RubyCocoa for iPhone - UICoderz4

irbうごかねーじゃん!!!

Page 15: Ruby/RubyCocoa for iPhone - UICoderz4

メモリ足りないで落ちる??

Page 16: Ruby/RubyCocoa for iPhone - UICoderz4

仕方無いのでeval.rbで楽しむ

Page 17: Ruby/RubyCocoa for iPhone - UICoderz4

…うむむ。

Page 18: Ruby/RubyCocoa for iPhone - UICoderz4

捨て

Page 19: Ruby/RubyCocoa for iPhone - UICoderz4

話は戻ってiPhoneSDK 2.x

Page 20: Ruby/RubyCocoa for iPhone - UICoderz4

ちょっと制限きつくなった

かも?

Page 21: Ruby/RubyCocoa for iPhone - UICoderz4

consoleがない

Page 22: Ruby/RubyCocoa for iPhone - UICoderz4

stdout/stderr はとりあえずXcodeのDebug出力

Page 23: Ruby/RubyCocoa for iPhone - UICoderz4

動的リンクできない

Page 24: Ruby/RubyCocoa for iPhone - UICoderz4

全部static link

Page 25: Ruby/RubyCocoa for iPhone - UICoderz4

アプリ外の領域のファイル読み書き不可

Page 26: Ruby/RubyCocoa for iPhone - UICoderz4

とかとか

Page 27: Ruby/RubyCocoa for iPhone - UICoderz4

configureとの格闘

Page 28: Ruby/RubyCocoa for iPhone - UICoderz4

とりあえずいらないヘッダ削る

Page 29: Ruby/RubyCocoa for iPhone - UICoderz4

削る

Page 30: Ruby/RubyCocoa for iPhone - UICoderz4

なんか要らないヘッダ

Page 31: Ruby/RubyCocoa for iPhone - UICoderz4

なんかマクロ

Page 32: Ruby/RubyCocoa for iPhone - UICoderz4

なんとか動いた (1.8.7)

Page 33: Ruby/RubyCocoa for iPhone - UICoderz4

samples/test.rbの8割動く

Page 34: Ruby/RubyCocoa for iPhone - UICoderz4

8割

Page 35: Ruby/RubyCocoa for iPhone - UICoderz4

Process.kill($$)できない

Page 36: Ruby/RubyCocoa for iPhone - UICoderz4

リソースフォルダにファイル書けない

Page 37: Ruby/RubyCocoa for iPhone - UICoderz4

外部のminiryby起動

できない

Page 38: Ruby/RubyCocoa for iPhone - UICoderz4

とりあえず動いたといっていいかな…

Page 39: Ruby/RubyCocoa for iPhone - UICoderz4

irbモドキをアプリにした

Page 40: Ruby/RubyCocoa for iPhone - UICoderz4
Page 41: Ruby/RubyCocoa for iPhone - UICoderz4

提出

Page 42: Ruby/RubyCocoa for iPhone - UICoderz4

待機

Page 43: Ruby/RubyCocoa for iPhone - UICoderz4

却下

Page 44: Ruby/RubyCocoa for iPhone - UICoderz4

理由

Page 45: Ruby/RubyCocoa for iPhone - UICoderz4

「どんなRubyのコードでも動いちゃう

っぽくね?(怒)」

Page 46: Ruby/RubyCocoa for iPhone - UICoderz4

(電卓とか作って出したらどうなるんだろう…)

Page 47: Ruby/RubyCocoa for iPhone - UICoderz4

あとで。

Page 48: Ruby/RubyCocoa for iPhone - UICoderz4

TODO

Page 49: Ruby/RubyCocoa for iPhone - UICoderz4

test/test_*.rb 動かす

Page 50: Ruby/RubyCocoa for iPhone - UICoderz4

irb動かす(ioをtelnet

で)

Page 51: Ruby/RubyCocoa for iPhone - UICoderz4

longjmp()なんか

おかしい問題

Page 52: Ruby/RubyCocoa for iPhone - UICoderz4

rubyの中だけで完結したraiseのrescueは問題ないっぽいが…

Page 53: Ruby/RubyCocoa for iPhone - UICoderz4

C側でrb_longjmpされたのが補足されないっぽい

Page 54: Ruby/RubyCocoa for iPhone - UICoderz4

組込

Page 55: Ruby/RubyCocoa for iPhone - UICoderz4

全部static linkにできたので

Page 56: Ruby/RubyCocoa for iPhone - UICoderz4

.h 5コぐらいと、.a 1コ

Page 57: Ruby/RubyCocoa for iPhone - UICoderz4

lib/用のtar1コ

(resource)

Page 58: Ruby/RubyCocoa for iPhone - UICoderz4

ruby_init()

rb_eval_string_protect()

Page 59: Ruby/RubyCocoa for iPhone - UICoderz4

Ruby Scriptを文字列で渡してやると実行してそのまま値を返す

Page 60: Ruby/RubyCocoa for iPhone - UICoderz4

くわしくはirbアプリのEvalRubyクラス参照

Page 61: Ruby/RubyCocoa for iPhone - UICoderz4

Page 62: Ruby/RubyCocoa for iPhone - UICoderz4

RubyCocoaのお話

Page 63: Ruby/RubyCocoa for iPhone - UICoderz4

RubyでCocoaを使える

Page 64: Ruby/RubyCocoa for iPhone - UICoderz4

?

Page 65: Ruby/RubyCocoa for iPhone - UICoderz4

ObjCな人から見たメリット(?)

Page 66: Ruby/RubyCocoa for iPhone - UICoderz4

Page 67: Ruby/RubyCocoa for iPhone - UICoderz4

ObjCメソッド呼び出し

Page 68: Ruby/RubyCocoa for iPhone - UICoderz4

[[UIViewalloc]initWithFrame(CGRectZero)]↓OSX::UIView.alloc.initWithFrame([0,0,0,0])

Page 69: Ruby/RubyCocoa for iPhone - UICoderz4

ほとんど書き方が一緒

Page 70: Ruby/RubyCocoa for iPhone - UICoderz4

かつ

Page 71: Ruby/RubyCocoa for iPhone - UICoderz4

[[ ]]

Page 72: Ruby/RubyCocoa for iPhone - UICoderz4

無い

Page 73: Ruby/RubyCocoa for iPhone - UICoderz4

!!!

Page 74: Ruby/RubyCocoa for iPhone - UICoderz4

Page 75: Ruby/RubyCocoa for iPhone - UICoderz4

つねにid型になってるイメージ

Page 76: Ruby/RubyCocoa for iPhone - UICoderz4

プリミティブ型は自動変換

Page 77: Ruby/RubyCocoa for iPhone - UICoderz4

構造体は?

Page 78: Ruby/RubyCocoa for iPhone - UICoderz4

配列からCGRectとか自動変換

Page 79: Ruby/RubyCocoa for iPhone - UICoderz4

delegationとか

どうなるの?

Page 80: Ruby/RubyCocoa for iPhone - UICoderz4

OSX::NSObjcetを継承したクラスは普通に受けられる

Page 81: Ruby/RubyCocoa for iPhone - UICoderz4

@interfaceHoge@end

@implementHoge‐applicationDidFinishLaunching(UIApplication*app){NSLog(“%s”,class_getName(app‐>isa));}@end

intmain(){returnUIApplicationMain(0,nil,nil,@”Hoge”);}

Page 82: Ruby/RubyCocoa for iPhone - UICoderz4

classHoge<OSX::NSObject defapplicationDidFinishLaunching(app) papp endend

OSX.UIApplicationMain(0,nil,nil,"Hoge")

Page 83: Ruby/RubyCocoa for iPhone - UICoderz4

classFuga<OSX::UIView definitialize @tv=OSX::UITableView.alloc.initWithFrame(OSX::UIScreen.mainScreen.bounds) @tv.setValue_forKey(self,"delegate") @tv.setValue_forKey(self,"dataSource") self.addSubview(@tv) end

deftableView_numberOfRowsInSection(tableView,section)return10end

deftableView_cellForRowAtIndexPath(tableView,indexPath)cellIdentifier="Cell"cell=tableView.dequeueReusableCellWithIdentifier(cellIdentifier)if(cell==nil)thencell=OSX::UITableViewCell.alloc.initWithFrame_reuseIdentifier([0,0,0,0],cellIdentifier)endcell.setValue_forKey(indexPath.row.to_s,"text")cellendend#thnaksto@hitoriblog

Page 84: Ruby/RubyCocoa for iPhone - UICoderz4

(そういえば)念願の

Page 85: Ruby/RubyCocoa for iPhone - UICoderz4

GC

Page 86: Ruby/RubyCocoa for iPhone - UICoderz4

参照カウンタ地獄からの

開放

Page 87: Ruby/RubyCocoa for iPhone - UICoderz4

そんなに上手くいかない?

Page 88: Ruby/RubyCocoa for iPhone - UICoderz4

欠点

Page 89: Ruby/RubyCocoa for iPhone - UICoderz4

ネイティブThread使えない

Page 90: Ruby/RubyCocoa for iPhone - UICoderz4

Rubyの使えばいいじゃない

Page 91: Ruby/RubyCocoa for iPhone - UICoderz4

t=Thread.newdop“hoge”end

t.join

Page 92: Ruby/RubyCocoa for iPhone - UICoderz4

Cocoaのセレクタ名が長いので

Page 93: Ruby/RubyCocoa for iPhone - UICoderz4

Rubyでも長さは変らないという話

Page 94: Ruby/RubyCocoa for iPhone - UICoderz4

‐tableView:(UITableView*)vcellForRowAtIndexPath:(NSIndexPath)i;

Page 95: Ruby/RubyCocoa for iPhone - UICoderz4

deftableView_cellForAtRowIndexPath(v,i)

Page 96: Ruby/RubyCocoa for iPhone - UICoderz4

どうしようもなさそう

Page 97: Ruby/RubyCocoa for iPhone - UICoderz4

プロパティのget/setの書き方長い

Page 98: Ruby/RubyCocoa for iPhone - UICoderz4

[UIApplicationsharedInstance].delegate↓OSX::UIApplication.sharedInstance.valueForKey(‘delegate’)

Page 99: Ruby/RubyCocoa for iPhone - UICoderz4

.delegate=self;↓.setValue_forKey(self,‘delegate’)

Page 100: Ruby/RubyCocoa for iPhone - UICoderz4

この書き方はプロパティと同じにできるかも?

Page 101: Ruby/RubyCocoa for iPhone - UICoderz4

ちなみに

Page 102: Ruby/RubyCocoa for iPhone - UICoderz4

HotCocoa.rb

Page 103: Ruby/RubyCocoa for iPhone - UICoderz4

MacRubyの一部分

PureRuby実装のCocoaラッパ

Page 104: Ruby/RubyCocoa for iPhone - UICoderz4

Page 105: Ruby/RubyCocoa for iPhone - UICoderz4

require'hotcocoa'includeHotCocoaappdowindow:title=>'HelloWorld',:frame=>[0,0,120,120]do|w|button:title=>'Clickme'do|b|b.on_action{puts'HelloWorld!'}w<<bendendend

Page 106: Ruby/RubyCocoa for iPhone - UICoderz4

詳しくは@lrzさんの資料参照

Page 108: Ruby/RubyCocoa for iPhone - UICoderz4

話は戻って

Page 109: Ruby/RubyCocoa for iPhone - UICoderz4

移植

Page 110: Ruby/RubyCocoa for iPhone - UICoderz4

最新(0.13.2)で

調査

Page 111: Ruby/RubyCocoa for iPhone - UICoderz4

まずlibffi動かないっぽい

Page 112: Ruby/RubyCocoa for iPhone - UICoderz4

orz

Page 113: Ruby/RubyCocoa for iPhone - UICoderz4

Page 114: Ruby/RubyCocoa for iPhone - UICoderz4

中川さん「古い実装ならlibffi使ってなかったと

思うよ?」

Page 115: Ruby/RubyCocoa for iPhone - UICoderz4

mjd

Page 116: Ruby/RubyCocoa for iPhone - UICoderz4

0.4.2まで使ってなかった

Page 117: Ruby/RubyCocoa for iPhone - UICoderz4

5年前の実装だけど0.4.2ベースで

Page 118: Ruby/RubyCocoa for iPhone - UICoderz4

ということで修正点

Page 119: Ruby/RubyCocoa for iPhone - UICoderz4

ObjC runtimeの構造体に直接をやめてAPI経由

にした

Page 120: Ruby/RubyCocoa for iPhone - UICoderz4

structobjc_methodstructobjc_ivar

Page 121: Ruby/RubyCocoa for iPhone - UICoderz4

とかはもはや使用できないらしい

Page 122: Ruby/RubyCocoa for iPhone - UICoderz4

動的クラス生成

Page 123: Ruby/RubyCocoa for iPhone - UICoderz4

objc_allocateClassPair()objc_registerClassPair()

Page 124: Ruby/RubyCocoa for iPhone - UICoderz4

クラスにメソッドを

追加

Page 125: Ruby/RubyCocoa for iPhone - UICoderz4

class_addMethod()

Page 126: Ruby/RubyCocoa for iPhone - UICoderz4

クラスにメンバ変数を追加

Page 127: Ruby/RubyCocoa for iPhone - UICoderz4

class_addIvar()

Page 128: Ruby/RubyCocoa for iPhone - UICoderz4

構造体などのスタブを生成しているRubyコードを修正

Page 129: Ruby/RubyCocoa for iPhone - UICoderz4

ObjC黒魔術vs

Ruby黒魔術

Page 130: Ruby/RubyCocoa for iPhone - UICoderz4

とりあえず

Page 131: Ruby/RubyCocoa for iPhone - UICoderz4

まだかなり不十分だけどhelloWorldレベルが動く

Page 132: Ruby/RubyCocoa for iPhone - UICoderz4

いちばんやりたかった事、それは…

Page 133: Ruby/RubyCocoa for iPhone - UICoderz4

リモートからirbで実機で動いてるアプリを動作中に変更する

Page 134: Ruby/RubyCocoa for iPhone - UICoderz4

Inspired byJSCocoaの

iPhone用インタラクティブコンソール

Page 135: Ruby/RubyCocoa for iPhone - UICoderz4

TCPサーバをRubyで書いてeval.rbに繋げただけ

Page 136: Ruby/RubyCocoa for iPhone - UICoderz4

(Macとかの) telnetから

コードを弄れる

Page 137: Ruby/RubyCocoa for iPhone - UICoderz4

デモ

Page 138: Ruby/RubyCocoa for iPhone - UICoderz4

githubのrepoで開発継続

Page 140: Ruby/RubyCocoa for iPhone - UICoderz4

今後

Page 141: Ruby/RubyCocoa for iPhone - UICoderz4

Page 142: Ruby/RubyCocoa for iPhone - UICoderz4

して

Page 143: Ruby/RubyCocoa for iPhone - UICoderz4

開発

Page 144: Ruby/RubyCocoa for iPhone - UICoderz4

可能

Page 145: Ruby/RubyCocoa for iPhone - UICoderz4

何か

Page 146: Ruby/RubyCocoa for iPhone - UICoderz4

纏め

Page 147: Ruby/RubyCocoa for iPhone - UICoderz4

Ruby楽しいよRuby

Page 148: Ruby/RubyCocoa for iPhone - UICoderz4

おしまい

Page 149: Ruby/RubyCocoa for iPhone - UICoderz4