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

Procのススメ/recommendation-of-proc

 Procのススメ/recommendation-of-proc

Masatoshi Moritsuka

December 14, 2019
Tweet

More Decks by Masatoshi Moritsuka

Other Decks in Programming

Transcript

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

    ➤ Qiita: @sanfrecce_osaka ➤ 9݄: େࡕ → ொా ➤ גࣜձࣾϥάβΠΞ ➤ ত࿨63೥ੜ·Ε ➤ ࣮࣭ฏ੒੎
  2. ஌Βͳ͍͏ͪʹ࢖͍ͬͯΔProc class ArticlesController < ApplicationController before_action -> do redirect_to root_path

    end def index # ॲཧ end end -> do redirect_to root_path end ͜ͷ෦෼΋Proc(lambda)
  3. 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
  4. ίϚϯυ૊Έཱͯ෦෼ΛϞδϡʔϧʹ੾Γग़͠ 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Λฦ͢ϝιουΛఆٛ
  5. Ճ޻ॲཧ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
  6. Method#to_procͷRuby࣮૷(̘࣮૷ͷίϝϯτ͔ΒҾ༻) class Method def to_proc lambda do |*_1| self.call(*_1) end

    end end શͯͷ
 ϒϩοΫҾ਺Λ ࣮ߦ͢Δϝιουͷ Ҿ਺ʹల։ શͯͷ
 ϒϩοΫҾ਺Λ
 splatͰ഑ྻʹ ϝιουΛ࣮ߦ
  7. 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)
  8. ʲRuby2.7.0ʙʳϝιουࢀরԋࢉࢠ titles .find { |title| … } .then(&Article.method(:new)) titles .find

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

    .find { |title| … } .then { Article.new(_1) } _1ͰୈҰϒϩοΫҾ਺ΛࢀরͰ͖Δ
  10. 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൪໨Ҏ߱ͷ
 ϒϩοΫҾ਺Λ
 ࣮ߦ͢Δϝιουͷ Ҿ਺ʹల։
  11. 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)
  12. Hash#to_procΛ࢖ͬͯΈΔ generations = { S: 'ত࿨', H: 'ฏ੒', R: 'ྩ࿨'

    } %i(S H).map(&generations) # => ['ত࿨', 'ฏ੒'] %i(S H).map { |key| generations[key] }
  13. ʲࢀߟʳ̘࣮૷ͰͷఆٛՕॴ(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ͷ஌ࣝͰಡΜͩͷͰఆٛՕॴҧ͏͔΋