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

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

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

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

やきとりい

January 16, 2014
Tweet

More Decks by やきとりい

Other Decks in Programming

Transcript

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


    #=> Rubyͷ಺෦࣮૷Λղઆʂ ͍ͭ΋ॻ͍͍ͯΔRubyͷίʔυ ͕ɺͲΜͳ෩ʹ࣮ߦ͞ΕΔͷ͔ ΛɺCͷίʔυͳ͠ͰɺਤೖΓ Ͱղઆͯ͘͠ΕΔຊ Ruby 2.0ରԠ
  2. 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͍Ζ͍Ζʣ ໨࣍ͬ͟ͱݟ
  3. ྫʣಉ͡Ϋϥεͷ̎ͭͷΦϒδΣΫτ 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
  4. δΣωϦοΫΦϒδΣΫτ
 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ͳͲ ୯७ͳ஋
  5. δΣωϦοΫΦϒδΣΫτͷ
 Πϯελϯεม਺ > 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”
  6. Πϯελϯεม਺ͷ୳͠ํ _ko1ઌੜߨٛϊʔτ value Flags valueͷflags ʹΑͬͯ
 ߏ଄ମΛ͔࣋ͭ൑ผ RObject δΣωϦοΫΦϒδΣΫτ simple

    ruby value ࣋ͨͳ͍ RBasicͷflagsͰ δΣωϦοΫΦϒδΣΫτ͔൑ผ ࣋ͭ flags klass RBasic ࣗ෼ͷ஋Λkeyʹɺgeneric_iv_tblΛ୳͢ ࣗ෼ͷclassͱivptr͔Β
 ୳͢
  7. 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
  8. Πϯελϯεϝιουͱ
 Πϯελϯεม਺໊ 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͸Ϋϥε΋ΦϒδΣΫτʂ
  9. Ϋϥεͷܧঝ 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:
  10. ΫϥεͷΠϯελϯεม਺ͱ Ϋϥεม਺ 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 ໰୊
  11. ΫϥεͷΠϯελϯεม਺ͱ Ϋϥεม਺ 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
  12. ΫϥεͷΠϯελϯεม਺ͱ Ϋϥεม਺ RClass: Mathematician ΫϥεϨϕϧΠϯελϯεม਺: “General” etc… DMBTT.BUIFNBUJDJBO !UZQFl(FOFSBMz FOE

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

    RClass: Mathematician ΫϥεϨϕϧΠϯελϯεม਺: “Statistic” etc… RClass: Statistician super ΫϥεϨϕϧΠϯελϯεม਺:
  14. ࣮ࡍͷΫϥεͷ಺෦ߏ଄ 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ʹ࢖͏
  15. Ϋϥεϝιου RClass: Mathematician klass RClass: Class m_tbl “class_method” ? DMBTT"OPUIFS$MBTTFOE

    "OPUIFS$MBTTDMBTT@NFUIPE VOEFpOFENFUIPE lDMBTT@NFUIPEzʜ MathematicianΫϥε͸Class ΫϥεͷΦϒδΣΫτ
 =>Ϋϥεϝιου͸Class Ϋϥεʹఆٛ͞ΕΔ
 #=> false!
  16. Ϋϥεϝιου:Ϋϥεͷੜ੒ 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
  17. Ϟδϡʔϧͷ಺෦ߏ଄ 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Λࢦఆ͢Δ͜ͱ͸ग़དྷͳ͍ʣ
  18. Ϟδϡʔϧͷϝιου୳ࡧॱ RClass: Mathematician super RClass: Person super RClass: Object NPEVMF1SPGFTTPSFOE

    ! DMBTT.BUIFNBUJDJBO1FSTPO JODMVEF1SPGFTTPS FOE RClass: AnotherSClass RClass: Professor super super 1 3 2 4 5
  19. Ϟδϡʔϧ…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)
  20. 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
  21. ϞδϡʔϧΛΠϯΫϧʔυͨ͠
 Ϟδϡʔϧ NPEVMF1SPGFTTPS JODMVEF&NQMPZFF FOE RClass: Professor super RClass: Employee

    super RClass: Employee super ίϐʔʂ Ϟδϡʔϧͷsuper͕includeͨ͠
 ϞδϡʔϧͷίϐʔΫϥεΛࢦ͢
  22. ϞδϡʔϧΛΠϯΫϧʔυͨ͠
 ϞδϡʔϧͷΠϯΫϧʔυ 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
  23. Ϟδϡʔϧ…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
  24. Ϟδϡʔϧ…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 ·Ͱ୳͞ͳ͍Ͱղܾ
  25. Ϟδϡʔϧ…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ͷΦϒδΣΫτ
 Ͳ͏΍ͬͯܧঝπϦʔΛલʹ໭Δͷʁ
  26. Ϟδϡʔϧ…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
  27. ໰୊: ΠϯΫϧʔυޙͷϞδϡʔϧ΁ͷ
 ϝιου௥Ճ NPEVMF1SPGFTPSFOE ! DMBTT.BUIFNBUJDJBO JODMVEF1SPGFTTPS FOE ! NPEVMF1SPGFTTPS

    EFGQSJNBSZ@DMBTTSPPN Ql"z FOE FOE ! QPJODBSF.BUIFNBUJDJBOOFX ! QPJODBSFQSJNBSZ@DMBTTSPPN ໰୊ ΠϯΫϧʔυޙʹϞδϡʔϧΛ
 ࠶Φʔϓϯͯ͠ϝιουΛ௥Ճ No Method Error? or “A-101”?