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

Automate the boring staff with Ruby

makicamel
December 27, 2020

Automate the boring staff with Ruby

- 2020.12.27. Tama.rb #25 年忘れLT会

makicamel

December 27, 2020
Tweet

More Decks by makicamel

Other Decks in Programming

Transcript

  1. ˞೥݄೔ݱࡏ "/%1"% $ fd application_controller.rb | wc -l # =>

    49 # => 325 ˞೥݄೔࣌఺ $ find . -name application_controller.rb | wc -l 85%ݮʂ ͍͢͝Ͷ
  2. DBTF •ActiveSupport::Concern ͷ included ϒϩοΫͷ࢖༻ΛݮΒ͍ͨ͠ module EnergeticGreeting extend ActiveSupport::Concern included

    do def hi p '٩(ˊᗜˋ*)و' end end end class GreetingA include EnergeticGreeting end GreetingA.new.hi # => "٩(ˊᗜˋ*)و" •include ͞Εͨ࣌ʹ ϒϩοΫ಺ͷίʔυ͕࣮ߦ͞ΕΔ
  3. module DepressedGreeting def hi p 'ʢ◞‸◟ʣ' end end class GreetingB

    include DepressedGreeting end GreetingB.new.hi # => “ʢ◞‸◟ʣ” includedϒϩοΫ •Πϯελϯεϝιου͸ Module#includeͰੜ΍ͤΔ
  4. module EnergeticGreeting extend ActiveSupport::Concern included do def hi p '٩(ˊᗜˋ*)و'

    end end end class GreetingA def hi p 'hi' end include EnergeticGreeting end GreetingA.new.hi # => "٩(ˊᗜˋ*)و" module DepressedGreeting def hi p 'ʢ◞‸◟ʣ' end end class GreetingB def hi p 'hi' end include DepressedGreeting end GreetingB.new.hi # => "hi" ʮਅͷϝιουఆٛʯ
  5. module EnergeticGreeting extend ActiveSupport::Concern included do def hi p '٩(ˊᗜˋ*)و'

    end end end class GreetingA def hi p 'hi' end include EnergeticGreeting end GreetingA.new.hi # => "٩(ˊᗜˋ*)و" ʮਅͷϝιουఆٛʯ module EnergeticGreeting extend ActiveSupport::Concern included do def hi p '٩(ˊᗜˋ*)و' end end end class GreetingA2 include EnergeticGreeting def hi p 'hi' end end GreetingA2.new.hi # => "hi"
  6. module EnergeticGreeting extend ActiveSupport::Concern included do def hi p '٩(ˊᗜˋ*)و'

    end end end class GreetingA def hi p 'hi' end include EnergeticGreeting end GreetingA.new.hi # => "٩(ˊᗜˋ*)و" ʮਅͷϝιουఆٛʯ module EnergeticGreeting extend ActiveSupport::Concern included do def hi p '٩(ˊᗜˋ*)و' end end end class GreetingA2 include EnergeticGreeting def hi p 'hi' end end GreetingA2.new.hi # => "hi"
  7. •Object#methods, Module#instance_methods •Ϩγʔό͕͍࣋ͬͯΔϝιου / ΠϯελϯεϝιουΛҰ๬͢Δ ΂ΜΓͳಓ۩ͨͪ 0CKFDUNFUIPET 3VCZϦϑΝϨϯεϚχϡΞϧ  IUUQTEPDTSVCZMBOHPSHKBNFUIPE0CKFDUJNFUIPETIUNM

    .PEVMFJOTUBODF@NFUIPET 3VCZϦϑΝϨϯεϚχϡΞϧ  IUUQTEPDTSVCZMBOHPSHKBNFUIPE.PEVMFJJOTUBODF@NFUIPETIUNM klass.instance_methods.select { |method| method.to_s.start_with? ‘awesome' }
  8. •Method#source_location •ϝιου͕ ruby Ͱఆٛ͞Ε͍ͯΔ৔߹ίʔυͷϑΝΠϧ໊ͱߦ൪߸Λฦ͢ •ϝιου͕ C Ͱఆٛ͞Ε͍ͯΔ৔߹ nil Λฦ͢ •e.g.

    ϓϩμΫγϣϯίʔυͰఆٛ͞Εͨಛఆͷ໊લͷϝιουΛநग़͢Δ ΂ΜΓͳಓ۩ͨͪ .FUIPETPVSDF@MPDBUJPO 3VCZϦϑΝϨϯεϚχϡΞϧ  IUUQTEPDTSVCZMBOHPSHKBNFUIPE.FUIPEJTPVSDF@MPDBUJPOIUNM if klass.instance_method(:awesome).source_location.first.include? "my_app"