Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WALKING AROUND THE RUBY FOREST

WALKING AROUND THE RUBY FOREST

the Guide to know Ruby implementation for non-C language programers
rubycont.tw 2014 presentation

やきとりい

April 27, 2014
Tweet

More Decks by やきとりい

Other Decks in Programming

Transcript

  1. 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ࢄาతᬐ੥
  2. զੋ...(I AM…) • ௗҪʢ࡫ాʣઇ / YUKI TORI • Rails application

    developer in Japan 
 (company: ສ༿ʣ • RailsGirls in JP organiser • Learned Programming by Rails
  3. 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
  4. 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త৿ྛੋ኷ਂ.ཁ஍ᅷ࿨ࢦೆ਑.
  5. 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.
  6. HE SEEMS ALWAYS ENJOYING RUBY IMPLEMENTATION. I ENVY HIM. 


    Ҽҝզతৎ෉
 ័ੋ؃ىདྷتᓣఔࣜRuby
  7. 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
  8. MAP: RUBY UNDER A MICROSCOPE • author: Pat Shaughnessy •

    ISBN: 9781593275273 • an Illustrated Guide
 to Ruby Internals.
  9. 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… Ṝ࿽ॻڅզ၇େൺྫჽత஍ᅷ
  10. MAP1: PARSE-COMPILE Class MyCode deff my … end ! …

    end Your Ruby Code Perse Compile AST ByteCode Code-> ந৅ߏจ໦ -> ByteCode
  11. 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
  12. 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!! Ṝ࿽ॻੋ࿝తࣕීวత
  13. 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
  14. 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 ઈ๬….
  15. 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” 彆灰⼼心.
  16. 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
  17. 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
  18. 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
  19. 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.
  20. 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”
  21. 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…
  22. 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…
  23. Class is also the object. =>It needs another Class 


    that storers the methods of the class object. $MBTT ʁ
  24. 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
  25. 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”
  26. 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. ᙘ㟬త஍ᅷಠࣗ஍