Slide 1

Slide 1 text

YUKI TORII(SASADA) 2014.04.26 RUBYCONF.TW WALK AROUND THE RUBY FOREST THE GUIDE TO KNOW RUBY IMPLEMENTATION FOR NON-C LANGUAGE PROGRAMERS Rubyࢄาతᬐ੥

Slide 2

Slide 2 text

զੋ...(I AM…) • ௗҪʢ࡫ాʣઇ / YUKI TORI • Rails application developer in Japan 
 (company: ສ༿ʣ • RailsGirls in JP organiser • Learned Programming by Rails

Slide 3

Slide 3 text

FIRST • This talk is for non C-programmer, knowing very little about language implementation
 (like me) .
 • The goal of this talk is:
 After you go home today, you start to open ruby source code file at glance. զઆ໘޲ non-C progammers

Slide 4

Slide 4 text

FIRST • Reading Ruby implementation is like walking through
 a dark, deep forest for beginners. • You had better to have maps and a compass for your exploration. • Let’t start to walk. 
 The forest hides many sweet fruits, and beautiful flowers. Rubyత৿ྛੋ኷ਂ.ཁ஍ᅷ࿨ࢦೆ਑.

Slide 5

Slide 5 text

WHY I’M TRYING TO READ RUBY IMPLEMENTATION. • I got married recently. • My husband is Koich Sasada (a.k.a. ko1).
 He implements Ruby as his job. ҝॄኄզࢼಡRuby಺෦ʁ
 Ҽҝզతৎ෉ੋRuby commiter.

Slide 6

Slide 6 text

HE SEEMS ALWAYS ENJOYING RUBY IMPLEMENTATION. I ENVY HIM. 
 Ҽҝզతৎ෉
 ័ੋ؃ىདྷتᓣఔࣜRuby

Slide 7

Slide 7 text

THE PREPARATION TO WALK INTO THE RUBY INTERNAL FOREST Rubyࢄาత४උ

Slide 8

Slide 8 text

BASIC PUT ON YOUR BOOTS, AND DO STRETCHING EXERCISES • Get a set of Ruby source
 
 • Put a first book in C-language beside of you
 (ask your friends or Google about good book.)
 > git clone https://github.com/ruby/ruby 
 ἥ্ۺࢠ: ಘ౸Ruby SourceCode

Slide 9

Slide 9 text

MAP&COMPASS • RUBY UNDER A MICROSCOPE • RUBY HACKING GUIDE ྆࿽ॻɿas ஍ᅷ࿨ࢦೆ਑

Slide 10

Slide 10 text

MAP: RUBY UNDER A MICROSCOPE • author: Pat Shaughnessy • ISBN: 9781593275273 • an Illustrated Guide
 to Ruby Internals.

Slide 11

Slide 11 text

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… Ṝ࿽ॻڅզ၇େൺྫჽత஍ᅷ

Slide 12

Slide 12 text

MAP1: PARSE-COMPILE Class MyCode deff my … end ! … end Your Ruby Code Perse Compile AST ByteCode Code-> ந৅ߏจ໦ -> ByteCode

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

MAP AND COMPASS: 
 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!! Ṝ࿽ॻੋ࿝తࣕීวత

Slide 15

Slide 15 text

WHYRHG IS USEFUL. • “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. Ṝ࿽ॻڭዎᒬ؃㣛SouceCode

Slide 16

Slide 16 text

MAP3: SOURCE FILE MAP BSDL COPYING COPYING.ja ChangeLog GPL KNOWNBUGS.rb LEGAL Makefile.in NEWS README.EXT README.EXT.ja README.ja.md README.md addr2line.c addr2line.h array.c benchmark bignum.c bin boots traptest class.c common.mk compar.c compile.c complex.c configure.in constant.h cont.c cygwin debug.c defs dir.c dln.c dln.h dln_find.c dmydln.c dmyext.c doc enc encoding.c enum.c en umerator.c error.c eval.c eval_error.c eval_intern.h eval_jump.c ext file.c gc.c gc.h gem_prelude.rb golf_prelude.rb goruby.c hash.c ia64.s include inits.c insns.def internal.h io.c iseq.c iseq.h lex.c.blt lib load.c loadpath.c localeinit.c main.c man marshal.c math.c method.h miniinit.c misc missing nacl node.c node.h numeric.c object.c pack.c parse.y prelude.rb probes.d probes_helper.h proc.c process.c random.c range.c rational.c re.c regcomp.c regenc.c regenc.h regerror.c regexec.c regint.h regparse.c regparse.h regsyntax.c ruby.c ruby_atomic.h safe.c sample signal.c siphash.c siphash.h sparc.c spec sprintf.c st.c strftime.c string.c struct.c symbian template test thread.c thread_native.h thread_pthread.c thread_pth read.h thread_win32.c thread_win32.h time.c timev.h tool transcode.c transcode_data.h util.c variable.c version.c version.h vm.c vm_backtrace.c vm_core.h vm_debug.h vm_dump.c vm_eval .c vm_exec.c vm_exec.h vm_insnhelper.c vm_insnhelper.h vm_method.c vm_opts.h vm_trace.c vsnprintf.c win32 > cd ./ruby; ls ઈ๬….

Slide 17

Slide 17 text

MAP3: SOURCE FILE MAP BSDL COPYING COPYING.ja ChangeLog GPL KNOWNBUGS.rb LEGAL Makefile.in NEWS README.EXT README.EXT.ja README.ja.md README.md addr2line.c addr2line.h array.c benchmark bignum.c bin boots traptest class.c common.mk compar.c compile.c complex.c configure.in constant.h cont.c cygwin debug.c defs dir.c dln.c dln.h dln_find.c dmydln.c dmyext.c doc enc encoding.c enum.c en umerator.c error.c eval.c eval_error.c eval_intern.h eval_jump.c ext file.c gc.c gc.h gem_prelude.rb golf_prelude.rb goruby.c hash.c ia64.s include inits.c insns.def internal.h io.c iseq.c iseq.h lex.c.blt lib load.c loadpath.c localeinit.c main.c man marshal.c math.c method.h miniinit.c misc missing nacl node.c node.h numeric.c object.c pack.c parse.y prelude.rb probes.d probes_helper.h proc.c process.c random.c range.c rational.c re.c regcomp.c regenc.c regenc.h regerror.c regexec.c regint.h regparse.c regparse.h regsyntax.c ruby.c ruby_atomic.h safe.c sample signal.c siphash.c siphash.h sparc.c spec sprintf.c st.c strftime.c string.c struct.c symbian template test thread.c thread_native.h thread_pthread.c thread_pth read.h thread_win32.c thread_win32.h time.c timev.h tool transcode.c transcode_data.h util.c variable.c version.c version.h vm.c vm_backtrace.c vm_core.h vm_debug.h vm_dump.c vm_eval .c vm_exec.c vm_exec.h vm_insnhelper.c vm_insnhelper.h vm_method.c vm_opts.h vm_trace.c vsnprintf.c win32 Don't give up your hope. File names are easy to understand. and README has
 “Appendix A. Ruby Source Files Overview” 彆灰⼼心.

Slide 18

Slide 18 text

MAP3: SOURCE FILE MAP ”Ruby Source Files Overview” in README.EXT groups source files by their contents. Ruby Language Core class.c :: classes and modules error.c :: exception classes and exception mechanism gc.c :: memory management load.c :: library loading object.c :: objects variable.c :: variables and constants Class Library array.c :: Array bignum.c :: Bignum compar.c :: Comparable complex.c :: Complex cont.c :: Fiber, Continuation dir.c :: Dir enum.c :: Enumerable enumerator.c :: Enumerator file.c :: File hash.c :: Hash io.c :: IO marshal.c :: Marshal math.c :: Math numeric.c :: Numeric, Integer, Fixnum, Float pack.c :: Array#pack, String#unpack proc.c :: Binding, Proc process.c :: Process random.c :: random number range.c :: Range rational.c :: Rational re.c :: Regexp, MatchData signal.c :: Signal sprintf.c :: String#sprintf string.c :: String struct.c :: Struct time.c :: Time Ruby Syntax Parser parse.y :: grammar definition parse.c :: automatically generated from parse.y keywords :: reserved keywords lex.c :: automatically generated from keywords Ruby Evaluator (a.k.a. YARV), Regular Expression Engine (Oniguruma), Utility Functions, Ruby Interpreter Implementation, Multilingualization, goruby Interpreter Implementation and other groups

Slide 19

Slide 19 text

MAP3: SOURCE FILE MAP File names are easy to understand. see the class libraries. Ruby Language Core class.c error.c gc.c load.c object.c variable.c Ruby Syntax Parser parse.y :: grammar definition parse.c :: automatically generated from parse.y keywords :: reserved keywords lex.c :: automatically generated from keywords Ruby Evaluator (a.k.a. YARV) Regular Expression Engine (Oniguruma) Utility Functions Ruby Interpreter Implementation Multilingualization goruby Interpreter Implementation and other groups Class Library array.c :: Array bignum.c :: Bignum compar.c :: Comparable complex.c :: Complex cont.c :: Fiber, Continuation dir.c :: Dir enum.c :: Enumerable enumerator.c :: Enumerator file.c :: File hash.c :: Hash io.c :: IO

Slide 20

Slide 20 text

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 dose…? 'PSFYBNQMF JGPOFEBZ*TUBSUUPXPOEFSJOH BOEDBOOPUTMFFQXFMM

Slide 21

Slide 21 text

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 do…? 'PSFYBNQMF JGPOFEBZ*TUBSUUPXPOEFSJOH BOEDBOOPUTMFFQXFMM See “hash.c”, Of course. and GoodNight.

Slide 22

Slide 22 text

COMPASS:METHOD DEFINITION Easy to find the definition of Hash#to_a in hash.c Because… ! WPJE *OJU@)BTI WPJE \ TOJQ ! SC@EFpOF@NFUIPE SC@D)BTI JOJUJBMJ[F SC@IBTI@JOJUJBMJ[F SC@EFpOF@NFUIPE SC@D)BTI JOJUJBMJ[F@DPQZ SC@IBTI@JOJUJBMJ[F@DPQZ SC@EFpOF@NFUIPE SC@D)BTI SFIBTI SC@IBTI@SFIBTI ! SC@EFpOF@NFUIPE SC@D)BTI UP@IBTI SC@IBTI@UP@IBTI SC@EFpOF@NFUIPE SC@D)BTI UP@I SC@IBTI@UP@I SC@EFpOF@NFUIPE SC@D)BTI UP@B SC@IBTI@UP@B SC@EFpOF@NFUIPE SC@D)BTI JOTQFDU SC@IBTI@JOTQFDU SC@EFpOF@BMJBT SC@D)BTI UP@T lJOTQFDU ! 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”

Slide 23

Slide 23 text

COMPASS:METHOD DEFINITION rb_define_method takes understandable args. TUBUJD7"-6& SC@IBTI@UP@B 7"-6&IBTI \ 7"-6&BSZ ! BSZSC@BSZ@OFX@DBQB 3)"4)@4*;& IBTI SC@IBTI@GPSFBDI IBTI UP@B@J BSZ 0#+@*/'&$5 BSZ IBTI ! SFUVSOBSZ ^ SC@EFpOF@NFUIPE SC@D)BTI UP@B SC@IBTI@UP@B 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…

Slide 24

Slide 24 text

൩҆… Good Night.

Slide 25

Slide 25 text

NOW, YOU GOT A READY.
 WOULD YOU LIKE TO
 WALK IN RUBY INTERNAL A LITTLE?

Slide 26

Slide 26 text

AN EXAMPLE ROUTE:
 ABOUT “CLASS” STRUCTURE. ! 0CKFDU4QBDFDPVOU@PCKFDUT<5@$-"44> ! DMBTT$FOE OJM ! 0CKFDU4QBDFDPVOU@PCKFDUT<5@$-"44> ʴ̎ Make a new Class Current total Class count Class count
 increase by…

Slide 27

Slide 27 text

$-"44$`4,-"4410*/5450 .&5"$-"44 Meta-class Created class “C”

Slide 28

Slide 28 text

8):
 l.&5"$-"44z&9*54

Slide 29

Slide 29 text

.&5)0%3&53*&7"- PCKFDU $MBTT “aaa”.size class String def size end end

Slide 30

Slide 30 text

Class is also the object. =>It needs another Class 
 that storers the methods of the class object. $MBTT ʁ

Slide 31

Slide 31 text

5)&l$-"44z*4.&5" $-"44 $MBTT .FUB
 $MBTT l4UPSFTUIF$MBTT.FUIPETPGUIF$MBTTz

Slide 32

Slide 32 text

the result of C.class ! $DMBTT 4PNF.FUB$MBTT ʷ ! $DMBTT $MBTT

Slide 33

Slide 33 text

*/5&3/"-4536$563&0' .&5"$-"44 $MBTT .FUB $MBTT 4VQFS $MBTT .FUBDMBTT PG 4VQFS$MBTT TVQFS DMBTT

Slide 34

Slide 34 text

C.class zSFUSJFWFT
 TVQFSDMBTTPG NFUBDMBTTVOUJM SFBDIFTBDUVBM DMBTT
 OPUNFUBDMBTT z DMBTT NFUB DMBTT TVQFS DMBTT NFUBDMBTT PG TVQFSDMBTT Object meta-class of Object Class class (actual- class) ʢ̍ʣ ʢ̎ʣ ʢ̏ʣ ʢ̐ʣ GOAL!! =>Class Start! ʼC.class

Slide 35

Slide 35 text

YOU CAN SEE THIS FLOW WITH THESE METHODS. • rb_obj_class / rb_class_real in “object.c” • rb_define_class • rb_define_class_id • rb_make_metaclass • make_metaclass • make_singleton_class etc… in “class.c”

Slide 36

Slide 36 text

LAST: DRAW YOUR OWN MAP • The map of two books are a part of the rich Ruby forest. • You can find your own favorite path, 
 or nice views by your self. ᙘ㟬త஍ᅷಠࣗ஍

Slide 37

Slide 37 text

ENJOY YOUR RUBY LIFE.
 THANK YOU. ։৺؝㠧ʂँँ.ɹ