Tianwen Chen
Developer
A Proc object is
an encapsulation of a block of code.
proc { |a| puts a }
a type of proc that:
a_proc = proc do |a, b|
puts a.inspect, b.inspect
end
a_proc.call('a')
# "a"
# nil
a_proc.call('a', 'b', 'c')
# "a"
# "b"
a_lambda = ->(a, b) do
puts a.inspect, b.inspect
end
a_lambda.call('a')
# ArgumentError (wrong number of arguments (given 1, expected 2))
a_lambda.call('a', 'b', 'c')
# ArgumentError (wrong number of arguments (given 3, expected 2))def show_me_the_return
-> { return 'lambda' }.call
proc { return 'proc' }.call
'end_of_method'
end
show_me_the_return
# => 'proc'Method objects are created by Object#method.
permit = ActionController::Parameters.new(a: 1).method(:permit)
# => #<Method: ActionController::Parameters#permit>
permit.call(:a)
# => <ActionController::Parameters {"a"=>1} permitted: true>
permit.source_location
# => ["lib/action_controller/metal/strong_parameters.rb", 528]method(:puts).object_id
# => 70131099412200
method(:puts).object_id
# => 70131111788740
method(:puts).object_id
# => 70131111773260permit = ActionController::Parameters.instance_method(:permit)
# => #<UnboundMethod: ActionController::Parameters#permit>
permit.call(:a)
# NoMethodError: undefined method `call' for
# #<UnboundMethod: ActionController::Parameters#permit>
# from (pry):88:in `<main>'
They are all
encapsulation of code
foo = 'bar'
a_proc = proc do
binding.local_variables
end
a_proc.call
# => [:a_proc, :foo]foo = 'bar'
def a_method
binding.local_variables
end
a_method
# => []
a_proc = proc { puts __method__ }
# => #<Proc:0x00000000038551@(pry):92>
define_method(:a_method, &a_proc)
# => :a_method
a_method
# a_method
# => nil
foo = 'bar'
def a_method; foo; end
a_method
# NameError (undefined local variable or method `foo' for main:Object)
foo = 'bar'
define_method(:b_method) { foo }
b_method
# => 'bar'
method(:puts).to_proc.call(1)
# 1
# => nil
class FormObject
def save
@collection.each do |item|
# TL;DR
end
end
end
class FormObject
def save
@collection.each &method(:convert)
end
def convert(item)
# TL;DR
end
end
Is the &method(:method_name) idiom bad for performance in Ruby?
√
method(:puts).to_proc.lambda?
# => true
def linear(x)
4 * x + 5
end
[1, 2, 3].map &method(:linear)def linear(a, b, x)
a * x + b
end
[1, 2, 3].map do |x|
linear 4, 5, x
end[1, 2, 3].map &method(:linear).curry.call(4, 5)method(:linear).curry-> do |a, b, c|
linear a, b, c
endmethod(:linear).curry.call(4, 5)-> do |a|
linear 4, 5, a
end[1, 2, 3].map(
&method(:linear).curry.call(4, 5)
)[1, 2, 3].map do |a|
linear 4, 5, a
enddef poisoning?(animal)
Snake === animal && animal.has_venom?
end
def cute?(animal)
Corgi === animal && animal.pure_bred?
end
case animal
when method(:poisoning?)
'run away'
when method(:cute?)
'give it a home'
endcase animal
when method(:poisoning?), 'spider'
'run away'
when method(:cute?)
'give it a home'
endif method(:poisoning?) === animal || \
'spider' === animal
'run away'
else method(:cute?) === animal
'give it a home'
endmethod(:poisoning?) === animalmethod(:poisoning?).call animaldef sanitize(data)
# ...
end
def transaform(data)
# ...
end
def parse(data)
# ...
end
list_of_data
.map(&method(:sanitize))
.map(&method(:transform))
.map(&method(:parse))
list_of_data
.map(
&method(:sanitize) >> \
method(:transform) >> \
method(:parse)
)
list_of_data.map do |data|
step1 = sanitize(data)
step2 = transform(step1)
parse(step2)
end
list_of_data
.map(
&method(:sanitize) << \
method(:transform) << \
method(:parse)
)
list_of_data.map do |data|
step1 = parse(data)
step2 = transform(step1)
sanitize(step2)
end
8.times.each &method(:puts)By Tianwen Chen