Ruby Under a Microscope 
を読んでみた (つまみぐい紹介編)

Ruby Under a Microscope 
を読んでみた (つまみぐい紹介編)

Ruby Under a Microscope 
を読んで面白かったので、一部の内容を社内勉強会「万葉究楽部」第四回で共有しました。

C67596c92e9d161d314f69de3f05f00c?s=128

やきとりい

January 16, 2014
Tweet

Transcript

  1. Ruby Under a Microscope 
 ΛಡΜͰΈͨ ʢͭ·Έ͙͍঺հฤʣ ສ༿ڀָ෦ #4 2014.01.16


    ௗҪઇ @yotii23
  2. جຊॻࢽ৘ใ ஶऀɿPat Shaughnessy ग़൛ࣾɿno starch press ISBNɿ9781593275273 O’REILLYɿ
 http://shop.oreilly.com/product/9781593275273.do

  3. ͲΜͳ಺༰ʁ "An Illustration Guide" 
 #=> ਤೖΓͰ "to Ruby Internals"


    #=> Rubyͷ಺෦࣮૷Λղઆʂ ͍ͭ΋ॻ͍͍ͯΔRubyͷίʔυ ͕ɺͲΜͳ෩ʹ࣮ߦ͞ΕΔͷ͔ ΛɺCͷίʔυͳ͠ͰɺਤೖΓ Ͱղઆͯ͘͠ΕΔຊ Ruby 2.0ରԠ
  4. 1: Tokenization and ParsingʢίʔυΛύʔπʹόϥͯ͠ɺߏจΛղੳ͢Δʣ 2: CompilationʢίϯύΠϧΑ͏͠ΒΜɺͱࢥͬͨΒKoichi Sasadaͷ໊લ͕ʣ 3: How Ruby

    Executes Your CodeʢRuby͸Ͳ͏΍ͬͯ͜ͷίʔυΛ࣮ߦͯ͠Δͷ͔ʣ 4: Control Structures and Method Dispatch ʢ੍ޚߏ଄ͱ͔ϝιουݺͼग़͠ͱ͔ʣ 5: Objects and ClassesʢΦϒδΣΫτͱΫϥεɺͳɻ͋Εͳɻʣ 6: Method Lookup and Constant Lookup ʢϝιου୳ࡧͱఆ਺୳ࡧʣ 7: The Hash Table: The Workhorse of Ruby Internals
 ʢϋογϡςʔϒϧɺRuby಺෦ͷ͸ͨΒ͖Ϟϊʣ 8: How Ruby Borrowed a Decades-Old Idea from Lisp
 ʢϒϩοΫ͸Rubyಠࣗͷಛ௃ͱ͔͡ΌͶ͔͑Βʂʣ 9: Metaprogramming ʢࠇຐज़ʣ 10: JRuby: Ruby on the JVM ʢJVMʣ, 11: Rubinius: Ruby Implemented with Ruby ʢRubiniusʣ 12: Garbage Collection in MRI, JRuby, and Rubinius ʢॲཧܥʹΑΔGC͍Ζ͍Ζʣ ໨࣍ͬ͟ͱݟ
  5. ΦϒδΣΫτͱΫϥε
 ɾΦϒδΣΫτͷ಺෦ߏ଄
 ɾΠϯελϯεม਺ͷ୳ࡧํ๏
 ɾΫϥεͷ಺෦ߏ଄ Ϟδϡʔϧ: include, extend, prepend ຊ೔ͷͭ·Έ͙͍ϝχϡʔ

  6. ΦϒδΣΫτͱΫϥε

  7. ΦϒδΣΫτͷ಺෦ߏ଄ จதఆٛ: “͢΂ͯͷRubyͷΦϒ δΣΫτ͸ɺΫϥε ϙΠϯλͱΠϯελ ϯεม਺ͷ഑ྻͷί ϯϏͰ͋Δ” ko1νΣοΫ:ϢʔβʔఆٛΫϥεͷ͜ͱɻ
 Πϯελϯεม਺഑ྻͷͳ͍ΦϒδΣΫτ΋͋Δ

  8. ΦϒδΣΫτͷ಺෦ߏ଄ RObject :ΦϒδΣΫτߏ଄ମ RBasic: ΦϒδΣΫτͷجຊཁૉ flags:಺෦తͳ͍ΖΜͳਅِ஋ͳͲ klass:classΛࣔ͢ϙΠϯλ numiv:Πϯελϯεม਺ͷ਺ ivptr:Πϯελϯεม਺ͷ഑ྻΛ ࣔ͢ϙΠϯλ

  9. ྫʣಉ͡Ϋϥεͷ̎ͭͷΦϒδΣΫτ class Mathematician attr_accesor :first_name attr_accesor :last_name end ! euler

    = Mathematician.new euler.first_name = “Leonhard” euler.last_name = “Euler” ! euclid = Mathematician.new euclid.first_name = “Euclid” RClass Mathematician RObject klass numiv: 2 ivptr RObject klass numiv: 1 ivptr Leonhard Euler Euclid
  10. δΣωϦοΫΦϒδΣΫτ
 simple ruby value String info… Array info… VALUE RSring

    flags klass RBasic VALUE RArray flags klass RBasic integer value Flags RObject δΣωϦοΫΦϒδΣΫτ FIXNUM_FLAG : 1 simple ruby value RObject numiv ivptr VALUE flags klass RBasic ଞɺRRegexp ͳͲ૊ΈࠐΈͷ
 ΦϒδΣΫτ ଞɺsymbolͳͲ ୯७ͳ஋
  11. δΣωϦοΫΦϒδΣΫτͷ
 Πϯελϯεม਺ > str = “ͳʹ͔จࣈྻ => “ͳʹ͔จࣈྻ” > str.instance_variables

    => [ ] > str.instance_variables_set(“@val1”, “value one”) => “value one” > str.instance_variables => [:@val1] RSring generic_iv_tbl “ͳʹ͔จࣈྻ” str => hash etc… etc… @val1 => “value 1” @val2 => “value 2”
  12. Πϯελϯεม਺ͷ୳͠ํ _ko1ઌੜߨٛϊʔτ value Flags valueͷflags ʹΑͬͯ
 ߏ଄ମΛ͔࣋ͭ൑ผ RObject δΣωϦοΫΦϒδΣΫτ simple

    ruby value ࣋ͨͳ͍ RBasicͷflagsͰ δΣωϦοΫΦϒδΣΫτ͔൑ผ ࣋ͭ flags klass RBasic ࣗ෼ͷ஋Λkeyʹɺgeneric_iv_tblΛ୳͢ ࣗ෼ͷclassͱivptr͔Β
 ୳͢
  13. genelic_iv_table͸஗͍ _ko1ઌੜߨٛϊʔτ Object String Regexp

  14. ivptr (instance variables pointer) Ruby1.8 Ruby1.9 RObject klass numiv: 2

    ivptr @val1 => “value 1” @val2 => “value 2” RClass RObject klass numiv: 2 ivptr ”value 1” “value 2” @val1 => 0 @val2 => 1
  15. ɹΈ͖ͯͨ͜ͱɿΦϒδΣΫτ ΦϒδΣΫτͷ಺෦ߏ଄
 ɾΫϥεϙΠϯλ
 ɾ͍͔ͭ͘ͷΦϒδΣΫτͷछྨͱߏ੒
 ɾΦϒδΣΫτม਺ͷΞΫηε

  16. Ϋϥεͷߏ଄ จதఆٛɿ ”Ϋϥε͸ϝιουఆٛɺ attribute namesɺεʔύʔ Ϋϥε΁ͷϙΠϯλɺఆ ਺ςʔϒϧΛ࣋ͬͨRuby ͷΦϒδΣΫτͰ͋Δ” http://www.flickr.com/photos/22326055@N06/5220652648/sizes/m/

  17. Πϯελϯεϝιουͱ
 Πϯελϯεม਺໊ DMBTT.BUIFNBUJDJBO EFGpSTU@OBNF !pSTU@OBNF FOE EFGpSTU@OBNF WBM  !pSTU@OBNFWBM

    FOE EFGMBTU@OBNF !MBTU@OBNF FOE EFGMBTU@OBNF WBM  !MBTU@OBNFWBM FOE FOE RClass flags klass RBasic method table first_name first_name= last_name last_name= attribute names @first_name @last_name Ruby͸Ϋϥε΋ΦϒδΣΫτʂ
  18. Ϋϥεͷܧঝ DMBTT.BUIFNBUJDJBO1FSTPO  ҎԼུʣ Q.BUIFNBUJDJBODMBTT $MBTT Q.BUIFNBUJDJBOTVQFSDMBTT 1FSTPO RClass RClass

    flags klass RBasic method table first_name first_name= last_name last_name= attribute names @first_name @last_name Class RClass Person super superclass:
  19. ΫϥεͷΠϯελϯεม਺ͱ Ϋϥεม਺ DMBTT.BUIFNBUJDJBO !UZQFl(FOFSBMz EFGTFMGUZQF !UZQF FOE FOE ! $MBTT4UBUJTUJDJBO.BUIFNBUJDJBO

    !UZQFl4UBUJTUJDTz FOE ! QVUT4UBUJTUJDJBOUZQF ໰୊ QVUT.BUIFNBUJDJBOUZQF ໰୊ DMBTT.BUIFNBUJDJBO !!UZQFl(FOFSBMz EFGTFMGUZQF !!UZQF FOE FOE ! $MBTT4UBUJTUJDJBO.BUIFNBUJDJBO !!UZQFl4UBUJTUJDTz FOE ! QVUT4UBUJTUJDJBOUZQF ໰୊ QVUT.BUIFNBUJDJBOUZQF ໰୊
  20. ΫϥεͷΠϯελϯεม਺ͱ Ϋϥεม਺ DMBTT.BUIFNBUJDJBO !UZQFl(FOFSBMz EFGTFMGUZQF !UZQF FOE FOE ! $MBTT4UBUJTUJDJBO.BUIFNBUJDJBO

    !UZQFl4UBUJTUJDTz FOE ! QVUT4UBUJTUJDJBOUZQF l4UBUJTUJDTz QVUT.BUIFNBUJDJBOUZQF l(FOFSBMz DMBTT.BUIFNBUJDJBO !!UZQFl(FOFSBMz EFGTFMGUZQF !!UZQF FOE FOE ! $MBTT4UBUJTUJDJBO.BUIFNBUJDJBO !!UZQFl4UBUJTUJDTz FOE ! QVUT4UBUJTUJDJBOUZQF l4UBUJTUJDTz QVUT.BUIFNBUJDJBOUZQF l4UBUJTUJDTz
  21. ΫϥεͷΠϯελϯεม਺ͱ Ϋϥεม਺ RClass ΫϥεϨϕϧ
 Πϯελϯεม਺: ˏtype @@type ͲͪΒ΋֨ೲ͞ΕΔςʔϒ ϧ͸Ұॹ ҧ͏ͷ͸ΞΫηεͷ࢓ํ

  22. ΫϥεͷΠϯελϯεม਺ͱ Ϋϥεม਺ RClass: Mathematician ΫϥεϨϕϧΠϯελϯεม਺: “General” etc… DMBTT.BUIFNBUJDJBO !UZQFl(FOFSBMz FOE

    DMBTT4UBUJTUJDJBO !UZQFl4UBUJTUJDTz FOE RClass: Statistician ΫϥεϨϕϧΠϯελϯεม਺: “Statistics” etc… ΫϥεͷΠϯελϯεม਺ @type
  23. ΫϥεͷΠϯελϯεม਺ͱ Ϋϥεม਺ RClass: Mathematician ΫϥεϨϕϧΠϯελϯεม਺: “General” etc… DMBTT4UBUJTUJDJBO !!UZQFl4UBUJTUJDTz FOE

    RClass: Statistician Ϋϥεม਺ @@type @@type͋Δʁ @@type͋Δʁ super ΫϥεϨϕϧΠϯελϯεม਺:
  24. ΫϥεͷΠϯελϯεม਺ͱ Ϋϥεม਺ DMBTT4UBUJTUJDJBO !!UZQFl4UBUJTUJDTz FOE ΫϥεͷΠϯελϯεม਺ @@type @@typeͳ͍… @@type͋Δ! ॻ͖׵͑Δʂ

    RClass: Mathematician ΫϥεϨϕϧΠϯελϯεม਺: “Statistic” etc… RClass: Statistician super ΫϥεϨϕϧΠϯελϯεม਺:
  25. ࣮ࡍͷΫϥεͷ಺෦ߏ଄ VALUE rb_classext_struct RClass RBasic flags klass m_tbl iv_index_tbl ptr

    super iv_tbl const_tbl origin refined_class allocator ΫϥεϙΠϯλ ϝιουςʔϒϧϙΠϯλ Πϯελϯεม਺໊ςʔϒϧ εʔύʔΫϥεϙΠϯλ ΫϥεϨϕϧΠϯελϯεม਺ɾΫϥεม਺ ఆ਺ςʔϒϧ ৽͍͠ΦϒδΣΫτͷϝϞϦ֬อʹ࢖͏ Module#prependʹ࢖͏ refinementsʹ࢖͏
  26. Ϋϥεϝιου Ϋϥεϝιου͸Ͳ͜ʹఆ ٛ͞ΕΔʁ DMBTT.BUIFNBUJDJBO DMBTTTFMG EFGDMBTT@NFUIPE QVUTlDMBTTNFUIPEz FOE FOE FOE

  27. Ϋϥεϝιου RClass: Mathematician klass RClass: Class m_tbl “class_method” ? DMBTT"OPUIFS$MBTTFOE

    "OPUIFS$MBTTDMBTT@NFUIPE VOEFpOFENFUIPE lDMBTT@NFUIPEzʜ MathematicianΫϥε͸Class ΫϥεͷΦϒδΣΫτ
 =>Ϋϥεϝιου͸Class Ϋϥεʹఆٛ͞ΕΔ
 #=> false!
  28. Ϋϥεϝιου:Ϋϥεͷੜ੒ 0CKFDU4QBDFDPVOU@PCKFDUT<5@$-"44>  DMBTT.BUIFNBUJDJBOFOE OJM 0CKFDU4QBDFDPVOU@PCKFDUT<5@$-"44> ໰୊ ObjectSpace.count_objects[:T_CLASS] … Ϋϥεͷ਺ΛΧ΢ϯτ͢Δ

    ໰୊ɿҰͭΫϥεΛ৽͘͠ੜ੒ͨ࣌͠ɺ
 Ϋϥεͷ૯਺͸͍ͭ͘૿͑Δ͔
  29. Ϋϥεϝιου:Ϋϥεͷੜ੒ 0CKFDU4QBDFDPVOU@PCKFDUT<5@$-"44>  DMBTT.BUIFNBUJDJBOFOE OJM 0CKFDU4QBDFDPVOU@PCKFDUT<5@$-"44>  ౴͑ɿ+ 2 ObjectSpace.count_objects[:T_CLASS]

    … Ϋϥεͷ਺ΛΧ΢ϯτ͢Δ
  30. Ϋϥεϝιου:Ϋϥεͷੜ੒ RObject klass RClass: Mathematician RClass method tablel first_name= first_name

    last_name= last_name flags klass RBasic flags klass RBasic method table class_method Singleton_class <Class: Mathematician> RClass: Class m_tbl m_tbl
  31. Έ͖ͯͨ͜ͱ2ɿΫϥε Ϋϥεͷ಺෦ߏ଄
 ɾΠϯελϯεม਺ͱΠϯελϯεϝιου
 ɾܧঝͷsuper
 ɾΫϥεͷΠϯελϯεม਺ʗΫϥεม਺ ΫϥεϝιουͱγϯάϧτϯΫϥε

  32. Ϟδϡʔϧ http://www.flickr.com/photos/glitchpunk/5320924205/sizes/m

  33. Ϟδϡʔϧͷ಺෦ߏ଄ m_tbl VALUE RClass:module rb_classext_struct RBasic flags klass ptr super

    iv_tbl const_tbl origin ModuleͰ͔ͭΘͳ͍΋ͷ ɾiv_index_tbl: Πϯελϯεม਺໊ςʔϒϧɹɹ ɾallocator: ৽͍͠ΦϒδΣΫτͷϝϞϦ֬ อ༻ 
 ※Ϟδϡʔϧ͸ΠϯελϯεΛ࡞Εͳ͍ ※಺෦తʹ͸super͸࣋ͭ
 ʢ೚ҙͷsuperΛࢦఆ͢Δ͜ͱ͸ग़དྷͳ͍ʣ
  34. Ϋϥεܧঝؔ܎ͷϝιου୳ࡧॱ RClass: Mathematician super RClass: Person super DMBTT.BUIFNBUJDJBO1FSTPO FOE RClass:

    AnotherSClass super RClass: Object super 1 3 2 4
  35. Ϟδϡʔϧͷϝιου୳ࡧॱ RClass: Mathematician super RClass: Person super RClass: Object NPEVMF1SPGFTTPSFOE

    ! DMBTT.BUIFNBUJDJBO1FSTPO JODMVEF1SPGFTTPS FOE RClass: AnotherSClass RClass: Professor super super 1 3 2 4 5
  36. Ϟδϡʔϧ…includeͷ࣮૷ RClass: Mathematician super RClass: Person super RClass: Professor RClass:

    AnotherSClass super RClass: Object super 1 3 2 4 5 RClass: Professor super inculde࣌ʹϞδϡʔϧͷίϐʔ͕ Ϋϥεͱͯ͠࡞੒͞Εɺ ܧঝπϦʔʹࠩ͠ࠐ·ΕΔ super ίϐʔʂ (module) (class)
  37. 1ͭͷΫϥεʹೋͭͷϞδϡʔϧ DMBTT.BUIFNBUJDJBO1FSTPO JODMVEF1SPGFTTPS JODMVEF&NQMPZFF FOE RClass: Mathematician super RClass: Person

    super RClass: Professor 1 4 2 RClass: Professor ίϐʔʂ super RClass: Employee RClass: Employee super 3
  38. ϞδϡʔϧΛΠϯΫϧʔυͨ͠
 Ϟδϡʔϧ NPEVMF1SPGFTTPS JODMVEF&NQMPZFF FOE RClass: Professor super RClass: Employee

    super RClass: Employee super ίϐʔʂ Ϟδϡʔϧͷsuper͕includeͨ͠
 ϞδϡʔϧͷίϐʔΫϥεΛࢦ͢
  39. ϞδϡʔϧΛΠϯΫϧʔυͨ͠
 ϞδϡʔϧͷΠϯΫϧʔυ NPEVMF1SPGFTTPS JODMVEF&NQMPZFF FOE DMBTT.BUIFNBUJDJBO1FSTPO JODMVEF1SPGFTTPS FOE RClass: Mathematician

    super RClass: Person super 1 4 2 ίϐʔʂ RClass: Professor super RClass: Employee super RClass: Professor super RClass: Employee super RClass: Employee super 3
  40. Ϟδϡʔϧ…extendͷ࣮૷ RObject: klass RClass: Person super 1 3 2 extend࣌ʹϞδϡʔϧͷίϐʔΫϥε͕


    ࡞੒͞ΕɺΦϒδΣΫτͷΫϥεʹͳΔ RClass: Professor RClass: Professor super super ίϐʔʂ (module) (class) RClass: Mathematician super NPEVMF1SPGFTTPSFOE DMBTT.BUIFNBUJDJBO1FSTPO FOE N.BUIFNBUJDJBOOFX NFYUFOE1SPGFTTPS
  41. Ϟδϡʔϧ…prepend (1. includeͰ͏·͘ߦ͔ͳ͍ྫ) NPEVMF1SPGFTPS EFGOBNF l1SPG\TVQFS^z FOE FOE ! DMBTT.BUIFNBUJDJBO

    BUUS@BDDFTTPSOBNF JODMVEF1SPGFTTPS FOE ! QPJODBSF.BUIFNBUJDBOOFX QPJODBSFOBNFl)FOSJ1PJODBSFz ! QQPJODBSFOBNF l)FOSJ1PJODBSFz ˌinclude ”Prof.”͕͔ͭͳ͍ RClass: Mathematician super RClass: Object super RClass: Professor super 1 2 3 attr_accessor: name def name “Prof. #{name}” end Mathematican͕ઌʹ୳͞ΕΔ
 …Professor ·Ͱ୳͞ͳ͍Ͱղܾ
  42. Ϟδϡʔϧ…prepend ʢ̎.prependͰղܾͨ͠ྫʣ NPEVMF1SPGFTPS EFGOBNF l1SPG\TVQFS^z FOE FOE ! DMBTT.BUIFNBUJDJBO BUUS@BDDFTTPSOBNF

    QSFQFOE1SPGFTTPS FOE ! QPJODBSF.BUIFNBUJDBOOFX QPJODBSFOBNFl)FOSJ1PJODBSFz ! QQPJODBSFOBNF l1SPG)FOSJ1PJODBSFz ˌprepend RClass: Mathematician super RClass: Object super RClass: Professor super 1 2 3 attr_accessor :name def name “Prof. #{name}” end Professor͕ઌʹ୳͞ΕΔ ٙ໰ɿpoincare͸MathematicianͷΦϒδΣΫτ
 Ͳ͏΍ͬͯܧঝπϦʔΛલʹ໭Δͷʁ
  43. Ϟδϡʔϧ…prependͷ࣮૷ RClass: Mathematician super RClass: Oject super RClass: Professor super

    attr_accessor :name def name “Prof. #{name}” end RClass: Mathematician super origin #prepend͸
 1. Mathematican
 ͷίϐʔ(OriginΫϥεʣΛɺ
 ΠϯΫϧʔυ͞ΕͨϞδϡʔϧͷ
 εʔύʔΫϥεʹ͢Δɻ 2. ΦϦδφϧͷΫϥεͷ
 શͯͷϝιουΛɺ
 OriginΫϥεʹҠ͢ Original Origin
  44. ໰୊: ΠϯΫϧʔυޙͷϞδϡʔϧ΁ͷ
 ϝιου௥Ճ NPEVMF1SPGFTPSFOE ! DMBTT.BUIFNBUJDJBO JODMVEF1SPGFTTPS FOE ! NPEVMF1SPGFTTPS

    EFGQSJNBSZ@DMBTTSPPN Ql"z FOE FOE ! QPJODBSF.BUIFNBUJDJBOOFX ! QPJODBSFQSJNBSZ@DMBTTSPPN ໰୊ ΠϯΫϧʔυޙʹϞδϡʔϧΛ
 ࠶Φʔϓϯͯ͠ϝιουΛ௥Ճ No Method Error? or “A-101”?
  45. ղ౴: ΠϯΫϧʔυޙͷϞδϡʔϧ΁ͷ
 ϝιου௥Ճ QPJODBSFQSJNBSZ@DMBTTSPPN l"z ΠϯΫϧʔυޙʹϞδϡʔϧʹ௥Ճ͞Εͨ
 ϝιου΋ݺͼग़ͤΔ
 ˎϝιουςʔϒϧͰ͸ͳ͘ɺ
 ϝιουςʔϒϧ΁ͷϙΠϯλΛίϐʔ͢ΔͨΊ method

    table primary_classroom RClass: Mathematician super ίϐʔʂ RClass: Professor super m_tbl RClass: Professor super m_tbl
  46. Έ͖ͯͨ͜ͱ3ɿ Ϟδϡʔϧ Ϟδϡʔϧͷ಺෦ߏ଄ include/extend/prepend ͷ࣮૷

  47. ·ͱΊ Ruby Under a Microscope : 
 Rubyͷϝδϟʔͳͱ͜Ζͷ಺෦ߏ଄Λେ·͔ʹཧղ ͤͯ͘͞ΕΔྑ͍ຊ ʮ͜͏͍͏͜ͱΛ͢Δͱ͜ͷڍಈʹͳΔʯͷཧ༝͕

    ෼͔ΔͱRubyͱ஥ྑ͘ͳΕΔؾ͕͢Δ ͝঺հͨ͠ͷ͸΄ΜͷҰ෦Ͱ͢