ruby extended library
DESCRIPTION
RubyKaigi 2008TRANSCRIPT
![Page 1: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/1.jpg)
RubyExtended Library Howto
artonalso not so known as Akio Tajima
![Page 2: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/2.jpg)
arton (Akio Rubyist Tajima ONline)ActiveScriptRuby
(Windows Installer Package with Ruby ActiveScript Interface)http://arton.hp.infoseek.co.jp/
Rjb(Ruby Java Bridge)http://rjb.rubyforge.org
diaryhttp://arton.no-ip.info/diary
![Page 3: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/3.jpg)
Agenda• Ruby 拡張ライブラリとは?• 何が嬉しいの?• 情報源• 簡単な拡張ライブラリのデモ• 特徴を生かすには?• それなりの拡張ライブラリのデモ• 代替案• まとめ• Q&A
![Page 4: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/4.jpg)
拡張ライブラリ
• 拡張ライブラリとは C またはC++ 言語で記述されていて、 Ruby に組み込むことのできるライブラリです。
オブジェクト指向スクリプト言語Ruby P.428
![Page 5: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/5.jpg)
何が嬉しいの?
• 拡張ライブラリによる部品を上手に設計し、重い処理の本質的な部分をそれらの部品に任せることができれば、コンパイラ型言語によるプログラムと大差ない実行速度と、比較にならない開発効率を両立させることも可能です。
上掲書 P.428
![Page 6: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/6.jpg)
高速化の例
1.upto(10) { |x| puts x }
for i in 1..10; puts i; end
for (i = 0; i < 10; i++) { rb_funcall(rb_stdout, rb_intern(“puts”), 1, INT2FIX(i));}
![Page 7: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/7.jpg)
情報源
• README.EXT 、 README.EXT.ja (1995)• オブジェクト指向スクリプト言語 Ruby
(1999)• http://www.loveruby.net/w/
RubyExtensionProgrammingGuide.html
• RHG
![Page 8: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/8.jpg)
一次情報• ruby.h• intern.h– 主要クラスの公開 API
• rubyio.h– (多分)
• rubysig.h– スレッド操作
• version.h• st.h– ハッシュ
![Page 9: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/9.jpg)
要求される技術• ポインタ–全部ポインタ
• ソースコードリーディング–Ruby のソースを読む
• セキュアコーディング
![Page 10: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/10.jpg)
配布
• setup.rb– http://www.loveruby.net/archive/setup/
• Gem– gem & rake
• Binary– MSI,apt,dmg
![Page 11: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/11.jpg)
デモ
• extrails– http://svn.arton.no-ip.info/raa/trunk/extrails
– Project generationextrails HelloWorld
» with Rakefile for creating Gem
– Skeleton generationruby script/generate scaffold Hello say_hello say_bye
» with sample code for defining variable, calling method
![Page 12: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/12.jpg)
拡張ライブラリの決まり
• void Init_ ファイル名 ()–モジュール、クラスの登録–定数、グローバル変数の作成
![Page 13: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/13.jpg)
定数、グローバル変数• 定数
rb_define_const( モジュール , “ 名前” , VALUE);rb_define_global_const(“ 名前” , VALUE);
• グローバル変数rb_define_variable(“ 名前” , VALUE*);
(GC 防御の基本 : cf. rb_gc_register_address(VALUE*))
rb_define_virtual_variable(“ 名前” , getter, setter);
![Page 14: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/14.jpg)
モジュール、クラス、メソッド
モジュール = rb_define_module(“ 名前” );rb_define_module_function( モジュール , “ 名前” ,
メソッド , arity);項数情報:
0 …… 無引数-1 …… 可変引数
クラス = rb_define_class(“ 名前” , super);クラス = rb_define_class_under( モジュール , “ 名前” ,
super);rb_define_method( クラス , “ 名前” , メソッド , arity);
![Page 15: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/15.jpg)
特徴を生かす
× テキスト処理× ネットワーク処理× ファイル操作
![Page 16: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/16.jpg)
特徴を生かす
○ アドレス操作○ 割り込み処理○ ネイティブ API
![Page 17: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/17.jpg)
デモ
• inspired by prof. Nakanishi’s Apple Lispfrom the article on the old magazin (bit)
• HeapShow– http://svn.arton.no-ip.info/raa/trunk/HeapShow
![Page 18: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/18.jpg)
RVALUE
HEAPS_SLOTVALUEVALUE
RVALUE (T_STRING) RVALUE
memory block : “ abcdefg……”
……
![Page 19: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/19.jpg)
苦労したところ=考慮点
• gc.c の static 変数や static 関数– extern にするのは簡単–でも、それで OK?–どうせ、依存性あるし……
–なぜ拡張ライブラリにするのかを考える• Do you need yet another Ruby ?
or you only need some other (maybe temporary) feature ?
![Page 20: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/20.jpg)
代替案• dl– native API for any external libraries
• COM component– win32ole– Windows only
• Java class– R jb etc– JRuby
![Page 21: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/21.jpg)
dl samplerequire 'dl/win32‘
GetShortPathName = Win32API.new('Kernel32.dll', 'GetShortPathNameA', 'SSI', 'I')
olen = 200 begin buff = ' ' * olen len = GetShortPathName.call(realpath.to_s, buff,
buff.size) if olen < len olen = len end end while olen == len buff.rstrip.chomp("\0")
// note: it seems bit buggy, but it’s correct.
![Page 22: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/22.jpg)
tips
• 拡張ライブラリを require する Ruby スクリプトを同時配布する–前処理–依存性の吸収(実際に require するライブラ
リの分岐など)–後処理(自動実行の制御など)
• バージョン番号を埋め込む
![Page 23: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/23.jpg)
まとめ
C programing is fun !
![Page 24: Ruby Extended Library](https://reader033.vdocuments.site/reader033/viewer/2022061119/5463cc60b4af9f3f3f8b46a4/html5/thumbnails/24.jpg)
Q&A