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

Procのススメ/recommendation-of-proc

 Procのススメ/recommendation-of-proc

5ddade6742b9a6ebd269ad0d7255158a?s=128

Masatoshi Moritsuka

December 14, 2019
Tweet

Transcript

  1. Procͷεεϝ ৿௩ɹਅ೥(@sanfrecce_osaka)
 ฏ੒Rubyձٞ01 #heiseirubykaigi

  2. ࣗݾ঺հ ➤ ৿௩ ਅ೥ ➤ GitHub: @sanfrecce-osaka ➤ Twitter: @sanfrecce_osaka

    ➤ Qiita: @sanfrecce_osaka ➤ 9݄: େࡕ → ொా ➤ גࣜձࣾϥάβΠΞ ➤ ত࿨63೥ੜ·Ε ➤ ࣮࣭ฏ੒੎
  3. ձࣾ঺հ ➤ RubyɾRailsͷडୗ։ൃձࣾ ➤ at ொా ➤ ձࣾͷۙลͰ΋Ո௞͕͍҆ʂ ➤ ະܦݧ͔ΒͰ΋2ϲ݄ͷݚम༗

    ➤ We are hiring!
  4. Thanks ➤ ਆށ.rb & ੢࿬.rbͷϝϯόʔ

  5. ͳͥProcͷ࿩ʁ

  6. ॻ͖ํͷखஈΛ ଟ͍࣋ͬͯ͘Δ 
 બ୒ࢶ͕ଟ͍
 
 ৔߹ʹԠͨ͡
 ॻ͖ํΛબ୒Ͱ͖Δ
 
 ίʔυͷ඼্࣭͕͕Δʂ

  7. ※஫ ࠓͷ͸͋͘·Ͱ ݸਓతݟղͰ͢

  8. Proc ࢖ͬͯ·͔͢ʁ

  9. ஌Βͳ͍͏ͪʹ࢖͍ͬͯΔProc class ArticlesController < ApplicationController before_action -> do redirect_to root_path

    end def index # ॲཧ end end -> do redirect_to root_path end ͜ͷ෦෼΋Proc(lambda)
  10. ࣮͸Α͘ݟΔ͜ͷॻ͖ํ΋ɾɾɾ map(&:to_s)

  11. &म০ࢠ ➤ ࣮Ҿ਺ͷ࠷ޙͰ&Λ෇͚Δͱto_proc͕ݺ͹ΕΔ ➤ to_procΛ࣋ͭΫϥε ➤ Proc(lambda)ɾMethodɾSymbolɾHash ➤ ֤ΫϥεͰڍಈ͕ҟͳΔ

  12. Proc#to_proc

  13. Proc#to_procͷRuby࣮૷ͷΠϝʔδ class Proc def to_proc self end end

  14. Proc#to _procΛ࢖༻ͨ͠ྫͷ঺հ(ཁ݅) ૹΒΕ͖ͯͨ
 ࣸਅΛม׵ ҹ࡮σʔλ(16ຕͷγʔϧ) ݩը૾

  15. MiniMagick::ToolΛ࢖༻ͯ͠ը૾Ճ޻ # ը૾Λճసͤ͞Δॲཧ MiniMagick::Tool::Convert.new do |command| command << 'images/input.jpg' command.merge!

    %w[-rotate 90] command << 'images/output.jpg' end # ը૾Λ੾ΓऔΔॲཧ MiniMagick::Tool::Convert.new do |command| command << 'images/input.jpg' command.merge! %w(-crop 900x500+170+0) command << 'images/output.jpg' end
  16. ίϚϯυ૊Έཱͯ෦෼ΛϞδϡʔϧʹ੾Γग़͠ def build_command -> (command) do args.each { |arg| add_arg(command,

    arg) } end end def add_arg(command, arg) command.send(merge_method(arg), arg) end def merge_method(arg) arg.kind_of?(Array) ? :merge! : :<< end ProcΛฦ͢ϝιουΛఆٛ
  17. Ճ޻ॲཧBeforeAfter MiniMagick::Tool::Convert.new do |command| command << 'images/input.jpg' command.merge! %W[-rotate 90]

    command << 'images/output.jpg' end def execute MiniMagick::Tool::Convert.new(&build_command) end def args [input_path, rotate_arg, output_path] end def rotate_arg %w[-rotate 90] end ໋໊ʹΑΓ
 ԿΛ͢Δͷ͔Ұ໨ͰΘ͔Δ Before After
  18. ࠷ऴతͳݺͼग़͠ଆ [ Cutting.new, Rotation.new, Montage.new, Positioning.new ].each(&:execute)

  19. Method#to_proc

  20. Method#to_procͷRuby࣮૷(̘࣮૷ͷίϝϯτ͔ΒҾ༻) class Method def to_proc lambda do |*_1| self.call(*_1) end

    end end શͯͷ
 ϒϩοΫҾ਺Λ ࣮ߦ͢Δϝιουͷ Ҿ਺ʹల։ શͯͷ
 ϒϩοΫҾ਺Λ
 splatͰ഑ྻʹ ϝιουΛ࣮ߦ
  21. Method#to_procΛ࢖ͬͯΈΔ [8, 0, 10] .map .with_index(1, &Report.method(:new)) # => [#<Report

    @hour=8, @day=1>,…] [8, 0, 10] .map .with_index(1) do |*args| Report.new(*args) end <= ೔෇ॱʹۈ຿࣌ؒΛ֨ೲͨ͠഑ྻ ex. ϧʔϓ1ճ໨: Report.new(8, 1)
  22. Method#to_proc͕ศརͳ৔໘ ➤ ໋໊ʹ࢖͏໊લΛઅ໿͍ͨ͠ͱ͖ ➤ ໋໊ʹ͔͔Δ࣌ؒɾ࿑ྗΛݮΒ͍ͨ͠ͱ͖ ➤ ໋໊͢Δࡍ͸ͳΔ΂͖ͪ͘Μͱ໊ͨ͠લΛ͚͍ͭͨ৔߹ ➤ ex. είʔϓ͕୹͍ͱͯ͠΋1จࣈͷ໊લ͸࢖͍ͨ͘ͳ͍

  23. Method#to_procͷ࢖͍Έͪ title = titles.find { |title| … } Article.new(title) titles

    .find { |title| … } .then(&Article.method(:new))
  24. ʲRuby2.7.0ʙʳϝιουࢀরԋࢉࢠ titles .find { |title| … } .then(&Article.method(:new)) titles .find

    { |title| … } .then(&Article.:new) .:method_name == .method(:method_name)
  25. revert͞Ε·ͨ͠(ůЧ`)ŢŞŢŞ

  26. ʲRuby2.7.0ʙʳNumbered Parameters titles .find { |title| … } .then(&Article.method(:new)) titles

    .find { |title| … } .then { Article.new(_1) } _1ͰୈҰϒϩοΫҾ਺ΛࢀরͰ͖Δ
  27. ʲTIPSʳprintσόοάͰͷར༻ users .reject(&:admin?) .tap(&method(:p)) .reject(&:retired?)

  28. Symbol#to_proc

  29. Symbol#to_procͷRuby࣮૷ class Symbol def to_proc proc do |_1, *_2| _1.method(self)

    .call(*_2) end end end ࠷ॳͷϒϩοΫҾ਺
 ͕Ϩγʔό &෇͖Ͱ౉ͨ͠
 Symbolͷ໊લͷ
 ϝιουΛ࣮ߦ 2൪໨Ҏ߱ͷ
 ϒϩοΫҾ਺͸
 splatͰ഑ྻʹ 2൪໨Ҏ߱ͷ
 ϒϩοΫҾ਺Λ
 ࣮ߦ͢Δϝιουͷ Ҿ਺ʹల։
  30. Symbol#to_procΛ࢖ͬͯΈΔ [10, 20, 30] .map .with_index(1, &:+) # => [11,

    22, 33] [10, 20, 30] .map .with_index(1) do |n1, *n2| n1.+(*n2) end ex. ϧʔϓ1ճ໨: 10.+(1)
  31. ͲΜͳ৔໘Ͱ ࢖͑Δʁ

  32. (ƅшƅ)ɾɾɾ

  33. ʢʀ^ω^ʣ

  34. Symbol#to_procʹҾ਺Λෳ਺౉͢ػձ͸΄ͱΜͲͳͦ͞͏ ➤ ༗ޮͳϢʔεέʔε͕͋·Γͳ͍ ➤ ୈೋϒϩοΫҾ਺ʹ഑ྻ౉ͯ͠ల։ɾɾɾ͸Ͱ͖ͳ͍ ➤ ϒϩοΫҾ਺ʹ౉ͬͨ࣌఺ͰҰ౓഑ྻʹ֨ೲ͞ΕΔͨΊ ➤ EnumeratorΛ֦ுͨ͠Βϫϯνϟϯ͋ΔՄೳੑ

  35. Hash#to_proc

  36. Hash#to_procͷRuby࣮૷ͷΠϝʔδ class Hash def to_proc proc do |_1| self[_1] end

    end end
  37. Hash#to_procΛ࢖ͬͯΈΔ generations = { S: 'ত࿨', H: 'ฏ੒', R: 'ྩ࿨'

    } %i(S H).map(&generations) # => ['ত࿨', 'ฏ੒'] %i(S H).map { |key| generations[key] }
  38. ͲΜͳͱ͖ʹ خ͍͠ͷʁ

  39. JSON΍CSVΛ ѻ͏ࡍʹ ͨ·ʹศར

  40. ࣌ؒ಺ʹ͓͞·Βͳͯ͘ল͍ͨ΋ͷ ➤ ಠࣗʹto_procΛఆٛ ➤ ؔ਺߹੒(Proc#>>ɺProc#<<) ➤ ϝιουͷҾ਺ʹϒϩοΫ(proc)Λ౉͢ ➤ Enumerator::Lazy

  41. ଓ͖͸QiitaͰʂ

  42. ʲࢀߟʳ̘࣮૷ͰͷఆٛՕॴ(2019/12/14࣌఺) ➤ Ҿ਺ͷύʔεՕॴ ➤ parse.y#L2422 ➤ Proc#to_proc ➤ proc.c#L1408 ➤

    Symbol#to_proc ➤ proc.c#L791 ➤ Method#to_proc ➤ proc.c#L2880 ➤ Hash#to_proc ➤ proc.c#L2880 ➤ ஫ɿઙ͍Cͷ஌ࣝͰಡΜͩͷͰఆٛՕॴҧ͏͔΋
  43. ࠷ޙʹ ➤ ࠓճ঺հͨ͠ςΫχοΫ͸޷͖ݏ͍͕෼͔ΕΔ ➤ ਓʹΑͬͯಡΈ΍͍͢ɾಡΈਏ͍ͷج४͕ҟͳΔ ➤ νʔϜͰೝࣝ͋ΘͤΛ͓ͯ͘͠ͱྑͦ͞͏

  44. ͝ਗ਼ௌ ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ʂ