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. 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
  2. Copyright © Kakaku.com Inc. All Rights Reserved. method_missing • BasicObjectΫϥεʹఆٛ͞Ε͍ͯΔprivateͳΠϯελϯεϝιου

    BasicObject.private_instance_methods.grep(/^method/) => [:method_missing] • ݺͼग़͞Εͨϝιου͕ɺܧঝπϦʔΛḷͬͯ୳ࡧͨ݁͠ՌͲ͜ʹ ΋ఆٛ͞Ε͍ͯͳ͔ͬͨ৔߹ɺmethod_missing͕ݺͼग़͞Ε NoMethodErrorΛൃੜͤ͞Δ
  3. 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 ϝιουΛݺͼग़͠·ͨ͠
  4. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ • ΰʔετϝιου͸respond_to?Ͱ͸falseͱͳΔ

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

    respond_to_missing?ϝιουʢObjectΫϥεͷprivateΠϯελϯε ϝιουʣΛΦʔόʔϥΠυ͠Α͏
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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>)
  11. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷࠇຐज़ͬ΀Γ “BasicObject͸respond_to?ϝιουΛ͍࣋ͬͯͳ͍ɻ ʢதུʣ

    respond_to_missing?ϝιου͕ෆཁʹͳΔͷͰɺ࡟আՄೳͩɻ” ʢϝλϓϩάϥϛϯάRuby ୈ2൛ 3ষΑΓʣ
  12. 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>)
  13. Copyright © Kakaku.com Inc. All Rights Reserved. method_missingͷΦʔόʔϥΠυ • ఆٛ͞Ε͍ͯͳ͍ϝιουʢΰʔετϝιουʣΛݺͼग़ͤΔΑ͏ʹͳΔ

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

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

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