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

メソッド探しの旅

 メソッド探しの旅

Ebisu.rb #27 ( https://ebisurb.connpass.com/event/238505/ )で発表した内容です。

Terai Shogo

February 10, 2022
Tweet

More Decks by Terai Shogo

Other Decks in Programming

Transcript

  1. 3 ΫϥεͱΦϒδΣΫτͷؔ܎ class C def c_instance_method @my_var = 1 end

    end obj = C.new C @my_var c_instance_method obj @my_var c_instance_method Ϋϥε͔ΒΦϒδΣΫτΛੜ੒
  2. 4 ΫϥεͱΦϒδΣΫτͷؔ܎ class C def c_instance_method @my_var = 1 end

    end obj = C.new C @my_var c_instance_method obj @my_var c_instance_method Ϋϥε͔ΒΦϒδΣΫτΛੜ੒ C c_instance_method obj @my_var Ϋϥε΁ͷࢀর Ϋϥε͸ 
 ΠϯελϯεϝιουΛ࣋ͭ ΦϒδΣΫτ͸ 
 Πϯελϯεม਺ͱ 
 Ϋϥε΁ͷࢀরΛ࣋ͭ
  3. 7 ϝιου୳ࡧͷجຊ class C def c_instance_method @my_var = 1 end

    end obj = C.new obj.c_instance_method C c_instance_method obj @my_var • ࢀর͍ͯ͠ΔΫϥεʹϝιουΛ୳͠ʹߦ͘ • ݟ͔ͭͬͨΒ࣮ߦ Ϋϥε΁ͷࢀর
  4. 8 ΦϒδΣΫτΛ2ͭੜ੒ͨ͠ͱ͖ class C def c_instance_method @my_var = 1 end

    end obj = C.new obj.c_instance_method obj2 = C.new obj2.c_instance_method obj @my_var • Πϯελϯεϝιου͸Ϋϥεʹ͋Γɺ 
 ڞ௨ͷϝιουΛ࣮ߦ͢Δ • Πϯελϯεม਺͸ΦϒδΣΫτʹ͋Γɺ 
 ͦΕͧΕผͷ΋ͷͰ͋Δ C c_instance_method obj2 @my_var
  5. 9 ݺͼग़ͨ͠ϝιου͕ଘࡏ͠ͳ͍ͱ͖ class C def c_instance_method @my_var = 1 end

    end obj = C.new obj.missing_method # ଘࡏ͠ͳ͍ϝιου • ϝιου͕ݟ͔ͭΒͳ͚Ε͹ɺsuperclassΛ 
 ͨͲͬͯ୳ࡧ͍ͯ͘͠ • BasicObject·Ͱ୳ࡧͯ͠΋ݟ͔ͭΒͳ͚Ε͹ 
 NoMethodError obj @my_var C c_instance_method Ϋϥε΁ͷࢀর Object superclass BasicObject superclass ݟ͔ͭΒͳ͔ͬͨͷͰ 
 superclassΛ୳ࡧ BasicObject·Ͱ 
 ݟ͔ͭΒͳ͔ͬͨͷͰ 
 NoMethodError
  6. 10 ΫϥεΛܧঝͨ͠ͱ͖ class C def c_instance_method @my_var = 1 end

    end class D < C; end obj = D.new obj.c_instance_method • ·ͣ͸ΦϒδΣΫτͷࢀর͍ͯ͠ΔΫϥεΛ୳ࡧ͢Δ • ݟ͔ͭΒͳ͚Ε͹superclassΛ୳ࡧ͢Δ • ݟ͔ͭΕ͹ͦ͜Ͱ࣮ߦ͢Δ obj @my_var D Ϋϥε΁ͷࢀর superclass ݟ͔ͭͬͨͷͰ 
 ୳ࡧऴྃ C superclass Object superclass BasicObject c_instance_method
  7. 11 ΦʔόʔϥΠυͨ͠ͱ͖ class C def c_instance_method @my_var = 1 end

    end class D < C def c_instance_method @my_var = 2 end end obj = D.new obj.c_instance_method obj @my_var D Ϋϥε΁ͷࢀর superclass ઌʹݟ͚ͭΒΕΔͨΊ 
 ͪ͜Β͕࣮ߦ͞ΕΔ C superclass Object superclass BasicObject c_instance_method c_instance_method
  8. 12 ϞδϡʔϧΛincludeͨ͠ͱ͖ module M def m_method "m_method" end end class

    C include M def c_instance_method @my_var = 1 end end obj = C.new obj.m_method obj @my_var C Ϋϥε΁ͷࢀর include includeͨ͠Ϟδϡʔϧ͸ 
 1্ͭʹૠೖ͞ΕΔ M superclass Object superclass BasicObject m_method c_instance_method superclassΛ୳ࡧ͢Δ 
 ΑΓલʹϞδϡʔϧ͕ 
 ୳ࡧ͞ΕΔ
  9. module M1 def m1_method "m1_method" end end module M2 def

    m2_method "m2_method" end end class C include M1 include M2 def c_instance_method @my_var = 1 end end obj = C.new obj.m1_method 13 ϞδϡʔϧΛ2ͭincludeͨ͠ͱ͖ obj @my_var Ϋϥε΁ͷࢀর C c_instance_method M2 m2_method M1 m1_method Object BasicObject include include superclass superclass ݟ͔ͭͬͨͷͰ 
 ୳ࡧऴྃ • ޙʹincludeͨ͠Ϟδϡʔϧ͕ 
 ΑΓԼʹૠೖ͞ΕΔ • ઌʹ୳ࡧ͞ΕΔ • ͭ·ΓΦʔόʔϥΠυͰ͖Δ
  10. 14 ϞδϡʔϧΛprependͨ͠ͱ͖ module M def m_method "m_method" end end class

    C prepend M def c_instance_method @my_var = 1 end end obj = C.new obj.m_method obj @my_var M Ϋϥε΁ͷࢀর prepend prependͨ͠Ϟδϡʔϧ͸ 
 Լʹૠೖ͞ΕΔ C superclass Object superclass BasicObject c_instance_method m_method
  11. module M1 def m1_method "m1_method" end end module M2 def

    m2_method "m2_method" end end class C prepend M1 prepend M2 def c_instance_method @my_var = 1 end end obj = C.new obj.m1_method 15 ϞδϡʔϧΛ2ͭprependͨ͠ͱ͖ obj @my_var Ϋϥε΁ͷࢀর M2 m2_method M1 m1_method C c_instance_method Object BasicObject pretend pretend superclass superclass ݟ͔ͭͬͨͷͰ 
 ୳ࡧऴྃ • ޙʹprependͨ͠Ϟδϡʔϧ͕ 
 ΑΓԼʹૠೖ͞ΕΔ • ઌʹ୳ࡧ͞ΕΔ • ͭ·ΓΦʔόʔϥΠυͰ͖Δ
  12. 20 ಛҟϝιου͕͋Δ৔ॴ class C def c_instance_method @my_var = 1 end

    end obj = C.new def obj.obj_singleton_method "obj_singleton_method" end obj.obj_singleton_method obj @my_var #obj Ϋϥε΁ͷࢀর superclass ݟ͔ͭͬͨͷͰ ୳ࡧऴྃ C superclass Object superclass BasicObject c_instance_method obj_singleton_method ʢobj.singleton_classʣ
  13. 21 ಛҟϝιου͕͋Δ৔ॴ obj @my_var #obj Ϋϥε΁ͷࢀর superclass ݟ͔ͭͬͨͷͰ ୳ࡧऴྃ C

    superclass Object superclass BasicObject c_instance_method obj_singleton_method ʢobj.singleton_classʣ • ࣮͸ɺ֤ΦϒδΣΫτ͸ඞͣҰͭͷಛҟΫϥεΛ͍࣋ͬͯΔ • ͜Ε·Ͱϝιου୳ࡧͰ͸ಛҟΫϥεΛ࠷ॳʹ୳ࡧ͍ͯͨ͠ • ࠓ·Ͱ͸ಛҟΫϥεʹϝιου͕ݟ͔ͭΒͳ͔ͬͨͷͰ 
 ૉ௨Γ͍ͯͨ͠ • ಛҟϝιου͸ಛҟΫϥεʹଐ͢Δ • ಛҟΫϥεͷsuperclass͸ΦϒδΣΫτͷࢀর͍ͯ͠ΔΫϥε • ͭ·ΓಛҟΫϥεʹϝιου͕ݟ͔ͭΒͳ͚Ε͹ɺ 
 ࣍ͷ୳ࡧର৅͸ΦϒδΣΫτ͕ࢀর͍ͯ͠ΔΫϥεʹͳΔ • ͨͩ͠obj͕ࢀর͍ͯ͠ΔΫϥε͸CΫϥεͰ͋Δ
  14. 23 Ϋϥεϝιου class C def c_instance_method; end def self.c_class_method; end

    end obj = C.new obj.c_instance_method # => ᶃ nil ʢϝιου͕ଘࡏʣ obj.c_class_method # => ᶄ NoMethodError C.c_instance_method # => ᶅ NoMethodError C.c_class_method # => ᶆ nil ʢϝιου͕ଘࡏʣ • Ϋϥεϝιου͸Ϋϥε͔ΒࢀরͰ͖Δϝιου • Ϋϥε͔Βੜ੒ͨ͠ΦϒδΣΫτ͔Β͸ݺͼग़ͤͳ͍ • Ϋϥε΋ΦϒδΣΫτͰ͋Δ • ͭ·ΓɺΫϥεϝιουͱ͸Ϋϥεͱ͍͏ΦϒδΣΫτͷಛҟϝιουͰ͋Δ
  15. 24 ᶃΦϒδΣΫτ͔ΒΠϯελϯεϝιουΛݺΜͩͱ͖ obj.c_instance_method => nil ʢϝιου͕ଘࡏʣ obj @my_var #obj Ϋϥε΁ͷࢀর

    superclass C superclass Object superclass BasicObject c_instance_method ʢobj.singleton_classʣ • Ϩγʔό͸obj • ୳ࡧ͸objͷಛҟΫϥεͰ͋Δ#obj͔Β࢝·Δ • c_instance_method͕ݟ͔ͭΒͳ͍ͷͰ 
 ࣍͸superclassΛ୳ࡧ͢Δ • ݟ͔ͭͬͨͷͰϝιουΛ࣮ߦ͢Δ #C c_class_method ʢC.singleton_classʣ
  16. 25 ᶄΦϒδΣΫτ͔ΒΫϥεϝιουΛݺΜͩͱ͖ obj.c_class_method => NoMethodError obj @my_var #obj Ϋϥε΁ͷࢀর superclass

    C superclass Object superclass BasicObject c_instance_method ʢobj.singleton_classʣ • Ϩγʔό͸obj • ୳ࡧ͸objͷಛҟΫϥεͰ͋Δ#obj͔Β࢝·Δ • c_class_method͕ݟ͔ͭΒͳ͍ͷͰ 
 ࣍͸superclassΛ୳ࡧ͢Δ • BasicObject·Ͱ୳ͯ͠΋ݟ͔ͭΒͳ͍ͷͰ 
 NoMethodError #C c_class_method ʢC.singleton_classʣ
  17. 26 ᶅΫϥε͔ΒΠϯελϯεϝιουΛݺΜͩͱ͖ C.c_instance_method => NoMethodError obj @my_var • Ϩγʔό͸C •

    ୳ࡧ͸CͷಛҟΫϥεͰ͋Δ#C͔Β࢝·Δ • c_instance_method͕ݟ͔ͭΒͳ͍ͷͰ 
 ࣍͸superclassΛ୳ࡧ͢Δ • #Cͷsuperclass͸ʮCͷsuperclassͰ͋Δ 
 ObjectʯͷಛҟΫϥε#ObjectͰ͋Δ • ಉ͡ཁྖͰ#BasicObject·Ͱ୳ࡧ͢Δ • ಛҟΫϥε#BasicObjectͷsuperclass͸ 
 ClassΫϥε • Class → Module → Object → BasicObject 
 ͱ୳ࡧͯ͠ݟ͔ͭΒͳ͍ͷͰ 
 NoMethodError c_class_method Object BasicObject #Object #BasicObject c_class_method Module Class C #obj #C Ϋϥε΁ͷࢀর superclass superclass superclass superclass superclass superclass superclass ʢobj.singleton_classʣ ʢC.singleton_classʣ ʢObject.singleton_classʣ ʢBasicObject .singleton_classʣ superclass
  18. 27 ᶆΫϥε͔ΒΫϥεϝιουΛݺΜͩͱ͖ C.c_class_method => nil ʢϝιου͕ଘࡏʣ obj @my_var • Ϩγʔό͸C

    • ୳ࡧ͸CͷಛҟΫϥεͰ͋Δ#C͔Β࢝·Δ • c_class_method͕ݟ͔ͭͬͨͷͰ࣮ߦ c_class_method Object BasicObject #Object #BasicObject c_class_method Module Class C #obj #C Ϋϥε΁ͷࢀর superclass superclass superclass superclass superclass superclass superclass ʢobj.singleton_classʣ ʢC.singleton_classʣ ʢObject.singleton_classʣ ʢBasicObject .singleton_classʣ superclass
  19. 29 ᶄΦϒδΣΫτ͔ΒΫϥεϝιουΛݺΜͩͱ͖ obj.c_class_method => NoMethodError obj @my_var #obj Ϋϥε΁ͷࢀর superclass

    C superclass Object superclass BasicObject c_instance_method ʢobj.singleton_classʣ • Ϩγʔό͸obj • ୳ࡧ͸objͷಛҟΫϥεͰ͋Δ#obj͔Β࢝·Δ • c_class_method͕ݟ͔ͭΒͳ͍ͷͰ 
 ࣍͸superclassΛ୳ࡧ͢Δ • BasicObject·Ͱ୳ͯ͠΋ݟ͔ͭΒͳ͍ͷͰ 
 NoMethodError #C c_class_method ʢC.singleton_classʣ
  20. 30 ᶅΫϥε͔ΒΠϯελϯεϝιουΛݺΜͩͱ͖ C.c_instance_method => NoMethodError obj @my_var • Ϩγʔό͸C •

    ୳ࡧ͸CͷಛҟΫϥεͰ͋Δ#C͔Β࢝·Δ • c_instance_method͕ݟ͔ͭΒͳ͍ͷͰ 
 ࣍͸superclassΛ୳ࡧ͢Δ • #Cͷsuperclass͸ʮCͷsuperclassͰ͋Δ 
 ObjectʯͷಛҟΫϥε#ObjectͰ͋Δ • ಉ͡ཁྖͰ#BasicObject·Ͱ୳ࡧ͢Δ • ಛҟΫϥε#BasicObjectͷsuperclass͸ 
 ClassΫϥε • Class → Module → Object → BasicObject 
 ͱ୳ࡧͯ͠ݟ͔ͭΒͳ͍ͷͰ 
 NoMethodError c_class_method Object BasicObject #Object #BasicObject c_class_method Module Class C #obj #C Ϋϥε΁ͷࢀর superclass superclass superclass superclass superclass superclass superclass ʢobj.singleton_classʣ ʢC.singleton_classʣ ʢObject.singleton_classʣ ʢBasicObject .singleton_classʣ superclass
  21. 31 ࢀߟʹ͍͍ͤͯͨͩͨ͞ॻ੶ɾهࣄ • ϝλϓϩάϥϛϯάRubyୈ2൛ • 2ষ ΦϒδΣΫτϞσϧ • 5ষ Ϋϥεఆٛ

    • ѹ౗௾άϥϯύͷϒϩά • ʲRubyʳϝιου୳ࡧ͔ΒݟΔɺϞδϡʔϧɾಛҟϝιουɾಛҟΫϥε