Βͳ͍͏ͪʹ͍ͬͯΔProc
class ArticlesController < ApplicationController
before_action -> do
redirect_to root_path
end
def index
# ॲཧ
end
end
-> do
redirect_to root_path
end
͜ͷ෦Proc(lambda)
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
Slide 16
Slide 16 text
ίϚϯυΈཱͯ෦ΛϞδϡʔϧʹΓग़͠
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Λฦ͢ϝιουΛఆٛ
Slide 17
Slide 17 text
Ճॲཧ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
Method#to_procͷRuby࣮(̘࣮ͷίϝϯτ͔ΒҾ༻)
class Method
def to_proc
lambda do |*_1|
self.call(*_1)
end
end
end
શͯͷ
ϒϩοΫҾΛ
࣮ߦ͢Δϝιουͷ
Ҿʹల։
શͯͷ
ϒϩοΫҾΛ
splatͰྻʹ
ϝιουΛ࣮ߦ
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൪Ҏ߱ͷ
ϒϩοΫҾΛ
࣮ߦ͢Δϝιουͷ
Ҿʹల։