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

method_missingの黒魔術っぷりについて

Sho Nagata
January 29, 2019

 method_missingの黒魔術っぷりについて

Ebisu.rb #21での発表資料

Sho Nagata

January 29, 2019
Tweet

More Decks by Sho Nagata

Other Decks in Programming

Transcript

  1. method_missingͷࠇຐज़ͬ΀Γʹ͍ͭͯ 2019/1/29 Ebisu.rb #21 גࣜձࣾΧΧΫίϜ ৯΂ϩάγεςϜຊ෦ ϚΠΫϩαʔϏεԽνʔϜ ௕ాɹᠳ

  2. Copyright © Kakaku.com Inc. All Rights Reserved. ࣗݾ঺հ ௕ాɹᠳʢͳ͕ͨɹ͠ΐ͏ʣ •

    మಓܥSIerͰSEΛ7೥൒ • 2018೥9݄ʹΧΧΫίϜೖࣾ • όοΫΤϯυΤϯδχΞͱͯ͠৯΂ϩάͷϚΠΫϩαʔϏε ԽΛਪਐத • Ruby / Railsྺ͸5ϲ݄ • ݸਓతʹMachine Learning΍Blockchain৮ͬͨΓ • ޷͖ͳݴޠ͸Python • Ϛϥιϯ΍ͬͨΓΪλʔ΍ͬͨΓғޟ΍ͬͨΓ • 2ࣇͷ෕ ˏs_naga03 Sho Nagata nagata03
  3. Copyright © Kakaku.com Inc. All Rights Reserved. ࠓ೔࿩͢͜ͱ RubyΛ৮Γ࢝Ίͯ1ϲ݄ࠒʹ܁Γ޿͛ͨϝιουఆٛͱͷઓ͍

  4. Copyright © Kakaku.com Inc. All Rights Reserved. ࠓ೔࿩͢͜ͱ RubyΛ৮Γ࢝Ίͯ1ϲ݄ࠒʹ܁Γ޿͛ͨϝιουఆٛͱͷઓ͍ ͦ͜Ͱग़ձͬͨ೉ఢ…

  5. ࠇຐज़ɿmethod_missing

  6. ࠇຐज़ɿmethod_missing

  7. Copyright © Kakaku.com Inc. All Rights Reserved. ࢲͷઓಆྗ ઓಆྗɿ1480

  8. Copyright © Kakaku.com Inc. All Rights Reserved. ࢲͷઓಆྗ ઓಆྗɿ1480 ʢϕδʔλऻདྷ࣌ʣ

    ʢDBެࣜʣ
  9. ࠇຐज़ɿmethod_missing

  10. ࠇຐज़ɿmethod_missing

  11. Copyright © Kakaku.com Inc. All Rights Reserved. Rubyྺ1ϲ݄ࠒͷ͜ͱ ݱࡏͷॲཧ಺༰ͷௐࠪ

  12. Copyright © Kakaku.com Inc. All Rights Reserved. Rubyྺ1ϲ݄ࠒͷ͜ͱ ݱࡏͷॲཧ಺༰ͷௐࠪ ϝιουΛݕࡧʢϝιου໊Ͱgrepͱ͔ʣ

  13. Copyright © Kakaku.com Inc. All Rights Reserved. Rubyྺ1ϲ݄ࠒͷ͜ͱ ݱࡏͷॲཧ಺༰ͷௐࠪ ϝιουΛݕࡧʢϝιου໊Ͱgrepͱ͔ʣ

    ϝιου͕ݟ͔ͭΓ·ͤΜʂ
  14. Copyright © Kakaku.com Inc. All Rights Reserved. Rubyྺ1ϲ݄ࠒͷ͜ͱ ݱࡏͷॲཧ಺༰ͷௐࠪ ϝιουΛݕࡧʢϝιου໊Ͱgrepͱ͔ʣ

    ϝιου͕ݟ͔ͭΓ·ͤΜʂ ίίʹ͋Γ·͢Αɻ
  15. Copyright © Kakaku.com Inc. All Rights Reserved. Rubyྺ1ϲ݄͝Ζͷ͜ͱ def method_missing(…)

    # … …… end
  16. Copyright © Kakaku.com Inc. All Rights Reserved. Rubyྺ1ϲ݄͝Ζͷ͜ͱ ԿॲͰ͔͢ʁ

  17. Copyright © Kakaku.com Inc. All Rights Reserved. Rubyྺ1ϲ݄͝Ζͷ͜ͱ ԿॲͰ͔͢ʁ Ͳ͜Ͱ͔͢ʁ

  18. Copyright © Kakaku.com Inc. All Rights Reserved. Rubyྺ1ϲ݄͝Ζͷ͜ͱ ԿॲͰ͔͢ʁ Ͳ͜Ͱ͔͢ʁ

    υίʂʁ
  19. Copyright © Kakaku.com Inc. All Rights Reserved. Rubyྺ1ϲ݄͝Ζͷ͜ͱ ԿॲͰ͔͢ʁ Ͳ͜Ͱ͔͢ʁ

    υίʂʁ ίίˣ def method_missing(…) # … …… end
  20. Copyright © Kakaku.com Inc. All Rights Reserved. Rubyྺ1ϲ݄͝Ζͷ͜ͱ

  21. Copyright © Kakaku.com Inc. All Rights Reserved. method_missing • BasicObjectΫϥεʹఆٛ͞Ε͍ͯΔprivateͳΠϯελϯεϝιου

    BasicObject.private_instance_methods.grep(/^method/) => [:method_missing] • ݺͼग़͞Εͨϝιου͕ɺܧঝπϦʔΛḷͬͯ୳ࡧͨ݁͠ՌͲ͜ʹ ΋ఆٛ͞Ε͍ͯͳ͔ͬͨ৔߹ɺmethod_missing͕ݺͼग़͞Ε NoMethodErrorΛൃੜͤ͞Δ
  22. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷΦʔόʔϥΠυ • method_missingΛΦʔόʔϥΠυ͢Δ͜ͱͰଘࡏ͠ͳ͍ϝιουΛ

    ݺͼग़ͤΔΑ͏ʹͳΔɹ-> ΰʔετϝιου
  23. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷΦʔόʔϥΠυ class Sample

    def method_missing(name) puts “͋ͳͨ͸ΰʔετϝιου #{name} ϝιουΛݺͼग़͠·ͨ͠” end end sample = Sample.new sample.ghost #=> ͋ͳͨ͸ΰʔετϝιου ghost ϝιουΛݺͼग़͠·ͨ͠
  24. method_missingͷࠇຐज़ͬ΀Γ

  25. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ • ΰʔετϝιου͸respond_to?Ͱ͸falseͱͳΔ

    sample = Sample.new sample.ghost #=> ͋ͳͨ͸ΰʔετϝιου ghost ϝιουΛݺͼग़͠·ͨ͠ sample.respond_to?(:ghost) #=> false
  26. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ • respond_to?ϝιουͰtrueΛฦ͢Α͏ʹ͢Δʹ͸

    respond_to_missing?ϝιουʢObjectΫϥεͷprivateΠϯελϯε ϝιουʣΛΦʔόʔϥΠυ͠Α͏
  27. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷΦʔόʔϥΠυ class DS

    def get_keyboard_info(computer_id); end end class Computer def initialize(id, data_source) @id = id @data_source = data_source end def method_missing(name) @data_source.send(“get_#{name}_info, @id end def respond_to_missing?(method, include_private = false) @data_source.respond_to?(“get_#{method}_info”) || super end end
  28. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ • ແݶϧʔϓʹೖͬͯ͠·͏Մೳੑ

  29. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ • ແݶϧʔϓʹೖͬͯ͠·͏Մೳੑ

    • super ೖΕΑ͏
  30. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ class Roulette

    def method_missing(name, *args) person = name.to_s.capitalize 3.times do number = rand(10) + 1 puts “#{number}…” end “#{person} got a #{number}” end end
  31. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ class Roulette

    def method_missing(name, *args) person = name.to_s.capitalize 3.times do number = rand(10) + 1 puts “#{number}…” end “#{person} got a #{number}” end end
  32. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ class Roulette

    def method_missing(name, *args) person = name.to_s.capitalize super unless %w[Bob Frank Bill].include? person number = 0 3.times do number = rand(10) + 1 puts “#{number}…” end “#{person} got a #{number}” end end
  33. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ • طଘͷϝιουΛݺͼग़͢Մೳੑ

    • undef ΍ ϒϥϯΫεϨʔτ ͰରԠ
  34. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ “ϒϥϯΫεϨʔτ͕ඞཁͰ͋Ε͹ɺBasicObjectΛ௚઀ܧঝ͢Ε͹͍͍ɻ” ʢϝλϓϩάϥϛϯάRuby

    ୈ2൛ 3ষΑΓʣ
  35. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ irb(main):002:0> cmp

    = Computer.new(@id, DS.new) (Object doesn't support #inspect) =>
  36. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ irb(main):002:0> cmp

    = Computer.new(@id, DS.new) (Object doesn't support #inspect) => irb(main):003:0> cmp.class NoMethodError (undefined method `class' for #<Computer: 0x00007fdc2d0e7a70>)
  37. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ “BasicObject͸respond_to?ϝιουΛ͍࣋ͬͯͳ͍ɻ ʢதུʣ

    respond_to_missing?ϝιου͕ෆཁʹͳΔͷͰɺ࡟আՄೳͩɻ” ʢϝλϓϩάϥϛϯάRuby ୈ2൛ 3ষΑΓʣ
  38. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ irb(main):003:0> cmp.respond_to?(:keyboard)

    NoMethodError (undefined method `respond_to?' for #<Com puter:0x00007fdc2d0e7a70>)
  39. ·ͱΊ

  40. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷΦʔόʔϥΠυ • ఆٛ͞Ε͍ͯͳ͍ϝιουʢΰʔετϝιουʣΛݺͼग़ͤΔΑ͏ʹͳΔ

    • ΰʔετϝιουʹ͍ͭͯrespond_to?ͰtrueΛฦ͢Α͏ʹ͍ͨ͠ͳΒ respond_to_missing?ϝιουΛΦʔόʔϥΠυ͠Α͏ • super Λ๨Εͣʹ • طଘͷϝιουΛݺͼग़ͯ͠͠·͏ՄೳੑΛ๨Εͣʹ
  41. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷΦʔόʔϥΠυ • ఆٛ͞Ε͍ͯͳ͍ϝιουʢΰʔετϝιουʣΛݺͼग़ͤΔΑ͏ʹͳΔ

    • ΰʔετϝιουʹ͍ͭͯrespond_to?ͰtrueΛฦ͢Α͏ʹ͍ͨ͠ͳΒ respond_to_missing?ϝιουΛΦʔόʔϥΠυ͠Α͏ • super Λ๨Εͣʹ • طଘͷϝιουΛݺͼग़ͯ͠͠·͏ՄೳੑΛ๨Εͣʹ
  42. Copyright © Kakaku.com Inc. All Rights Reserved. ࠷ޙʹ We are

    hiring! WEBɺΞϓϦɺϑϩϯτΤϯυΤϯδ χΞͳͲ༷ʑͳ৬छͰΤϯδχΞืू தͰ͢ʂ ΧδϡΞϧ໘ஊʢΦϑΟε๚໰ʣ׻ܴ Ͱ͢ʂ