YUKI TORII(SASADA) 2014.10.19 RUBYKAIGI WALK AROUND THE RUBY FOREST MORE DEEPLY. THE GUIDE TO KNOW RUBY IMPLEMENTATION FOR NON-C LANGUAGE PROGRAMERS ϧϏΟϊεεϝ
FIRST • This talk is for non C programmers/ beginners like me. • goal: After go back home tonight, you glance at Ruby’s source code. ͜ͷτʔΫॳ৺ऀ͚Ͱ͢ ʢࢲΈ͍ͨͳʣ
RUBY UNDER A MICROSCOPE WILL GIVE YOU… • a large scale Map about 2.0 Ruby Implementation. • Parsing, and Compilation of Ruby code • Structures of Ruby Basic objects • and etc…
MAP2:STRUCTURE OF OBJECTS String info… Array info… VALUE RSring flags klass RBasic VALUE RArray flags klass RBasic integer value Flags RObject built-in Object FIXNUM_FLAG : 1 simple ruby value RObject numiv ivptr VALUE flags klass RBasic and RRegexp etc… built-in objects and Symbol ect… Simple values
JAPANESE VER. IS COMING SOON! • ʰRubyͷ͘͠Έ Ruby Under a Microscopeʱ • translators: Koji Shimada Shintaro Kakutani • ൃߦɿΦʔϜࣾ։ൃ෦ • ץߦ༧ఆɿ2014ळ • I joins as one of reviewers. * ΖΖௐதͷͨΊ·ͩΦʔϜࣾʹ͓͍߹Θͤ͠ͳ͍Ͱ͍ͩ͘͞
MAP2: RUBY HACKING GUIDE(RHG) • author: Minero Aoki • Original is written in Japanese, and is translated into English. All contents are available at http://ruby-hacking- guide.github.io/ • Talking about very details on Ruby source code and implementation. • about Ruby ver. 1.7.3… old, but Very Very Useful!!
WHY RHG IS USEFUL? BECAUSE IT’S VERY UNIVERSAL. • “How to read source codes” • making a method simple by cutting not essential code off. • understanding object structure before. • and many other wisdoms • It was written for C-programmers. But non C-programmer can learn Basic C essence for reading Ruby Implementation. essence
WHY RHG IS USEFUL? BECAUSE IT’S SOMEHOW OLD. • Ruby is improving. • While reading Ruby source code with RHG, you will find a difference between the description and the current code. => This is a chance to know Why the source has been changed. It’s a trace of improvement of ruby. You can trace it by ChangeLog and with its commit message.
MAP3: SOURCE FILE MAP extension also give us hints. xx.c => Of cause c code xx.h => header file. Many definitions such as macros, data-structures. xx.y => only parse.y (grammar definition) xx.rb => Of cause ruby file. xx.def => miscellaneous definitions
MAP3: SOURCE FILE MAP /include/ … many ruby’s core header files. /defs/ … definition files such as keywords, errors… /ext/ … c extensions such as json, socket… /lib/ …ruby libraries such as csv, irb, yaml, rake… /missing/ …implements of C functions need for ruby, but not all Systems have. /test/ … test codes for ruby implementations. /spec/ … RubySpec will be downloaded here. Special directories.
COMPASS:METHOD DEFINITION Because some file names correspond to class names, it is easy to find where a method is defined. What Hash#to_a does…? 'PSFYBNQMF
JGPOFEBZ*TUBSUUPXPOEFSJOH BOEDBOOPUTMFFQXFMM
COMPASS:METHOD DEFINITION Because some file names correspond to class name, it is easy to find where a method is defined. What Hash#to_a does…? 'PSFYBNQMF
JGPOFEBZ*TUBSUUPXPOEFSJOH BOEDBOOPUTMFFQXFMM See “hash.c”, Of course. and GoodNight.
! TOJQNBOZPUIFSNFUIPEEFpOJUJPOT built-in class has Init_xx function at bottom of its file. }In the Init_xx, almost all methods are defined by “rb_define_method”
klass method_name function init arg function name is put on the head of line to search easily. make new array with hash size convert hash’s each pair to array Macro often defined in ***.h. but I can imagine how work from name…
WHAT IS A HASH TABLE. The data-structure for keeping key/value pair. obj of Hash class Method Table Key Value :favorite_animal “rabbit" “scores” [10,3,4] :neighborhood # Key Value “name” def name … “age” def age… “love?” def love?(me)….
RUBY HASH TABLE STRUCTURE bins entries st_table type num_bins bins (big) num_entries st_table_entry hash key record next fore back 1 2 3 4 next next fore back (hash is ordered from Ruby 1.9~) 5 6 7 entries_packed (0) back
YOU CAN FIND ALL OF THEM IN • st_table in “include/ruby/st.h” • st_table_entry /st_packed_entry • add_packed_direct • unpacked • add_direct • rehash in “st.c”
LAST:DRAW YOUR OWN MAY BY YOUR HAND • The map of two books are a part of the rich Ruby forest. • You can find your favorite path, or nice views by your self. ࣗͷਤΛ࡞ͬͯΈΑ͏