ruby enumerable source code reading
DESCRIPTION
20120811 るりま読書会 Enumerable編TRANSCRIPT
![Page 1: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/1.jpg)
Ruby enumerable source code
reading2012/08/11るりま読書会 Enumerator 編
do_aki
![Page 2: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/2.jpg)
@do_aki
http://do-aki.net/
![Page 3: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/3.jpg)
PHPer
I’m a PHPer
![Page 4: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/4.jpg)
![Page 5: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/5.jpg)
![Page 6: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/6.jpg)
閑話休題
![Page 7: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/7.jpg)
新たにプログラミング言語を学ぶ時の効果的な方法
![Page 8: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/8.jpg)
Code Reading
![Page 9: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/9.jpg)
Ruby のコード読んでやんよ
( `・ ω ・ ´)
![Page 10: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/10.jpg)
![Page 11: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/11.jpg)
ruby-1.9.3-
p194
![Page 12: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/12.jpg)
直下の *.c
Ruby 言語仕様組み込みライブラリ
標準添付ライブラリ
ext/lib 以下
![Page 13: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/13.jpg)
=> array.c=> hash.c=> enum.c=> enumerator.c
ArrayHash
EnumerableEnumerator
![Page 14: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/14.jpg)
array.cvoidInit_Array(void){ rb_cArray = rb_define_class("Array", rb_cObject); rb_include_module(rb_cArray, rb_mEnumerable);
rb_define_alloc_func(rb_cArray, ary_alloc); rb_define_singleton_method(rb_cArray, "[]", rb_ary_s_create, -1); rb_define_singleton_method(rb_cArray, "try_convert", rb_ary_s_try_convert, 1); rb_define_method(rb_cArray, "initialize", rb_ary_initialize, -1); rb_define_method(rb_cArray, "initialize_copy", rb_ary_replace, 1);
rb_define_method(rb_cArray, "inspect", rb_ary_inspect, 0); rb_define_alias(rb_cArray, "to_s", "inspect"); rb_define_method(rb_cArray, "to_a", rb_ary_to_a, 0); rb_define_method(rb_cArray, "to_ary", rb_ary_to_ary_m, 0); rb_define_method(rb_cArray, "frozen?", rb_ary_frozen_p, 0);
以下 rb_define_method が続く
![Page 15: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/15.jpg)
Global Objectsarray.c
=> VALUE rb_cArray;hash.c
=> VALUE rb_cHash;enum.c
=> VALUE rb_mEnumerable; enumerator.c
=> VALUE rb_cEnumerator; VALUE rb_eStopIteration;
![Page 16: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/16.jpg)
Enumerable を継承してるクラス
おまけ
![Page 17: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/17.jpg)
array.cvoidInit_Array(void){ rb_cArray = rb_define_class("Array", rb_cObject); rb_include_module(rb_cArray, rb_mEnumerable);
rb_define_alloc_func(rb_cArray, ary_alloc); rb_define_singleton_method(rb_cArray, "[]", rb_ary_s_create, -1); rb_define_singleton_method(rb_cArray, "try_convert", rb_ary_s_try_convert, 1); rb_define_method(rb_cArray, "initialize", rb_ary_initialize, -1); rb_define_method(rb_cArray, "initialize_copy", rb_ary_replace, 1);
rb_define_method(rb_cArray, "inspect", rb_ary_inspect, 0); rb_define_alias(rb_cArray, "to_s", "inspect"); rb_define_method(rb_cArray, "to_a", rb_ary_to_a, 0); rb_define_method(rb_cArray, "to_ary", rb_ary_to_ary_m, 0); rb_define_method(rb_cArray, "frozen?", rb_ary_frozen_p, 0);
以下 rb_define_method が続く
![Page 18: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/18.jpg)
るりまに載っていない隠しメソッドがあるのでは!?
![Page 19: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/19.jpg)
んなもんあるわきゃない
( ´ ・ ω ・`)けど、ちょっとだけ ものを発見キニナル
![Page 20: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/20.jpg)
Enumerator::Yieldernew {|y| ... } -> EnumeratorEnumerator オブジェクトを生成して返します。与えられたブロックは Enumerator::Yielder オブジェクトを 引数として実行されます。生成された Enumerator オブジェクトに対して each を呼ぶと、この生成時に指定されたブロックを 実行し、 Yielder オブジェクトに対して << メソッドが呼ばれるたびに、 each に渡されたブロックが繰り返されます。new に渡されたブロックが終了した時点で each の繰り返しが終わります。 このときのブロックの返り値が each の返り値となります。
![Page 21: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/21.jpg)
Enumerator::Yielder sampleenum = Enumerator.new do |
yielder| (1..3).each do |i| yielder << i endend
enum.each{|i| p i}# => 1 2 3
![Page 22: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/22.jpg)
Enumerator::Yielder sampleenum = Enumerator.new do |
yielder| (1..3).each do |i| yielder.yield i endend
enum.each{|i| p i}# => 1 2 3
UNDOCUMENTED!!!
![Page 23: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/23.jpg)
経緯ドキュメント無いから書くよ
現在の実装は実験的なもの明文化すると容易に変え難い
じゃぁいっか。
そこまでする必要なくね?
Yieldable module という案は?
http://ruby.11.n6.nabble.com/ruby-dev-39660-Bug-trunk-Enumerator-new-y-y-lt-lt-1-lt-lt-2-lt-lt-3-td3535850.html
![Page 24: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/24.jpg)
よーわからん
てか、そのへんドキュメントに書いとけよ
![Page 25: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/25.jpg)
時間切れ的まとめ的な何か
• 処理系読んでみると面白いよ
• コード自体は ruby script っぽい感じ=> ruby に慣れた人は読みやすい
ハズ
• より深く ruby を知ることができる
![Page 26: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/26.jpg)
ありがとうございました
![Page 27: Ruby enumerable source code reading](https://reader034.vdocuments.pub/reader034/viewer/2022051210/54b723754a7959cc1c8b456b/html5/thumbnails/27.jpg)
Question?