$30 off During Our Annual Pro Sale. View Details »

I finally understand instance_eval

I finally understand instance_eval

Masafumi Okura

February 01, 2020
Tweet

More Decks by Masafumi Okura

Other Decks in Programming

Transcript

  1. instance_eval׬શʹཧ
    ղͨ͠ɿinstance_exec
    ΋๨ΕΔͳฤ
    Speaker: େ૔խ࢙
    Event: Burikaigi2020
    Date: 2020-02-01

    View Slide

  2. self.inspect
    • ໊લɿେ૔խ࢙
    • ॴଐɿϑϦʔϥϯε
    • Rubyྺɿ໿7೥
    • ޷͖ͳΤσΟλɿVim

    View Slide

  3. self.inspect
    • ࠷ۙͷ՝୊ɿ໊ࢗͱϚελεϥΠυͷσβΠ
    ϯΛͲ͏͢Δ͔
    • झຯɿᘖᘣΛᔸΕΔʢࠓ͸ϑϨϯνϓϨεʣ
    • ޷͖ͳϝιουɿinstance_eval <- ࠓ೔ͷ࿩

    View Slide

  4. ͸͡Ίʹ

    View Slide

  5. ͜ͷτʔΫʹ͍ͭͯʢϝλʣ
    • εϥΠυຕ਺ɿ29ຕ
    • ࣌ؒɿ25෼
    • ओͳλʔήοτɿϝλϓϩάϥϛϯάॳ৺ऀ
    ͔Βதڃऀ
    • σϞ༗Γ

    View Slide

  6. ·ͣ͸͓͞Β͍

    View Slide

  7. instance_eval ͱ͸
    • eval଒ͷ1ͭͰɺ೚ҙͷΦϒδΣΫτ
    ʢBasicObjectؚΉʣͷίϯςΩετͰจࣈྻ
    ͷධՁ·ͨ͸ϒϩοΫͷ࣮ߦ͕Մೳ
    • DSLͷߏஙͳͲʹଟ༻͞ΕΔ
    • RubyͷॊೈੑΛࢧ͍͑ͯΔʢݸਓͷݟղʣ

    View Slide

  8. ॳֶऀ޲͚ิ଍ɿධՁͬͯʁ
    • eval଒͸จࣈྻΛRubyͷίʔυʹݟཱ࣮ͯͯ
    ߦ͢Δ
    • irb͕΍͍ͬͯΔ͜ͱɺͱ͍͏ͱΠϝʔδ͠΍
    ͍͔͢΋
    • ධՁ͸ӳޠͰevaluationͳͷͰॖΊͯeval

    View Slide

  9. ॳֶऀ޲͚ิ଍ɿϒϩοΫ
    • ϒϩοΫͷதͷίʔυ͸͙͢ʹ͸࣮ߦ͞Εͳ
    ͍ʢίʔϧόοΫ͞ΕΔɺͱ΋ݴ͏ʣ
    • ϒϩοΫ͸ͦͷ··Ͱ͸ม਺୅ೖͰ͖ͳ͍
    ͕ɺ&blockͳͲͰड͚ͨ΋ͷ͸୅ೖՄೳ
    • ϒϩοΫΛݺͼग़͢ʢcallͱ͔yieldͱ͔ʣͱ
    ͖ɺҾ਺Λ౉͢ͱͦΕ͕ϒϩοΫҾ਺ʹͳΔ

    View Slide

  10. instance_evalͱଞͷeval଒͸
    Ͳ͏ҧ͏ͷ͔
    • eval͸จࣈྻ͔͠ධՁͰ͖ͳ͍ʢϒϩοΫΛ࣮
    ߦͰ͖ͳ͍ʣ
    • class_eval͸ධՁͷίϯςΩετ͕ҧ͏
    • ʢeval଒Ͱ͸ͳ͍͚Ͳʣinstance_exec͸จࣈྻ
    ͷධՁ͕Ͱ͖ͳ͍ʢϒϩοΫͷ࣮ߦ͔͠Ͱ͖ͳ
    ͍ʣɺϒϩοΫҾ਺͕౉ͤΔ

    View Slide

  11. σϞͱ͍͏͔؆୯ͳ࢖͍ํ

    View Slide

  12. σϞ͔ΒΘ͔Δ͜ͱ
    • instance_evalʹ౉ͨ͠ϒϩοΫ಺Ͱ͸self͸Ϩ
    γʔόΛࢦ͍ͯ͠Δ
    • ͳͷͰɺϒϩοΫ಺ͰselfΛলུ͢ΔͱϨγʔό
    ͷϝιου͕ݺ΂Δ
    • self͕লུͰ͖ΔͷͰϓϥΠϕʔτϝιου΋Π
    ϯελϯεม਺΋ΞΫηε͠์୊

    View Slide

  13. ެࣜυΩϡϝϯτΛಡΉ

    View Slide

  14. ެࣜυΩϡϝϯτΛಡΉ
    • https://docs.ruby-lang.org/en/2.7.0/
    BasicObject.html#method-i-instance_eval
    • https://docs.ruby-lang.org/ja/2.7.0/class/
    BasicObject.html#I_INSTANCE_EVAL
    • ༨ஊͳ͕Βɺinstance_eval͸BasicObjectʹੜ
    ͍͑ͯΔ

    View Slide

  15. ཁ͢ΔʹԿ͕ॻ͍ͯ͋Δͷ͔
    • จࣈྻΛड͚Δͱ͖ͱϒϩοΫΛड͚Δͱ͖
    ͱͰڍಈ͕ҧ͏ʢ౰ͨΓલʣ
    • จࣈྻΛड͚Δͱ͖͸جຊతʹevalͱಉ͡ಈ
    ͖Λ͢Δ
    • ϒϩοΫΛड͚Δͱ͖͸ϒϩοΫΛ࣮ߦ͢Δ
    ࡍʹϒϩοΫҾ਺ʹϨγʔόΛ౉͢

    View Slide

  16. ॏཁͳ͜ͱ
    • ϒϩοΫʹҾ਺Λ౉ͤͳ͍
    • ϒϩοΫʹҾ਺Λ౉ͤͳ͍ <-େࣄͳ͜ͱ
    • DSLΛߏங͢Δࡍʹ࢖͍ͮΒ͍ͱ͖͕͋Δ

    View Slide

  17. ࣮ྫ

    View Slide

  18. ࣮ྫσϞ͔ΒΘ͔ͬͨ͜ͱ
    • instance_evalͱinstance_exec͸ڍಈ͕ҟͳ
    Γɺ༻్΋·ͨҟͳΔ
    • instance_exec͸ड͚ͨϒϩοΫͷҾ਺ʹ೚ҙͷ
    ΦϒδΣΫτΛ౉ͤΔ
    • ϒϩοΫΛ࣮ߦ͢Δ͚ͩͳΒinstance_execͷ΄
    ͏͕͍͍ͷ͔΋ʁ

    View Slide

  19. ͪΐͬͱ͓΋͠Ζ͍࿩

    View Slide

  20. ಛҟʢγϯάϧτϯʣϝιου
    • Πϯελϯεʹ͔͠ఆٛ͞Ε͍ͯͳ͍ϝιο
    υ
    • instance_evalΛ࢖͏ͱେྔੜ੒͕؆୯
    • ࣮͸Ϋϥεϝιουͱݺ͹Ε͍ͯΔ΋ͷ͸͜
    ͷҰछ

    View Slide

  21. ಛҟϝιουΛੜ΍ͯ͠ΈΔ

    View Slide

  22. instance_eval಺Ͱdef͕ݺ΂Δ
    • Կ͕ى͖͍ͯΔͷͩΖ͏͔
    • ͦͷṖΛ໌Β͔ʹ͢΂͘ɺզʑ͸RubyͷԞ஍
    ʢιʔείʔυʣ΁ͱཱཱྀͬͨ…

    View Slide

  23. RubyͷιʔείʔυΛಡΉ

    View Slide

  24. ιʔείʔυ͔ΒΘ͔Δ͜ͱ
    • CݴޠͰॻ͍ͯ͋ΔʢͦΕ͸ͦ͏ʣ
    • vm_eval.cͱ͍͏ϑΝΠϧʹinstance_evalͷ࣮
    ૷͕͋Δ
    • execution_contextͱ͍͏ͷ͕ॏཁͦ͏

    View Slide

  25. ೉͍͠

    View Slide

  26. RubiniusΛಡΜͰΈΔ

    View Slide

  27. Rubiniusͷinstance_evalΑΓ
    • RubyͷϒϩοΫ࣮ߦʹ͓͍ͯείʔϓ͸ඇৗ
    ʹॏཁʢʰϝλϓϩάϥϛϯάRubyʱಡ΋
    ͏ʣ
    • CRubyͱಡΈൺ΂Δͱ͍͍͔΋

    View Slide

  28. ·ͱΊ

    View Slide

  29. instance_evalָ͍͠

    View Slide