Dive in:

Proc & Method

@tian_im

Proc

A Proc object is

an encapsulation of a block of code.

0
 Advanced issues found
▲
2
0
 Advanced issues found
▲
2
 
 
proc { |a| puts a }

Lambda

a type of proc that:

  • Arguments are treated as strict
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))
  • return means to exit from this lambda
def show_me_the_return
  -> { return 'lambda' }.call
  proc { return 'proc' }.call
  'end_of_method'
end

show_me_the_return
# => 'proc'

Method

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]

!! it always returns a new method object !!

method(:puts).object_id
# => 70131099412200
method(:puts).object_id
# => 70131111788740
method(:puts).object_id
# => 70131111773260

UnboundMethod

permit = 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>'

UnboundMethod

Proc vs Method

They are all

encapsulation of code

Proc

  • Does not need to be associated with an object

Method

  • Cannot be called without an associated object

Proc

  • It has access to the entire context (including the local variables) where it's created

Method

  • When a method is defined using `def`, it only has access to its method scope
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
# => []

Proc <=> Method

define_method &proc

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#to_proc

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?

Pop Quiz

Method#to_proc is a kind of ?

  • Proc
  • Lambda
  • All of the Above
  • None of the Above

√

method(:puts).to_proc.lambda?
# => true

#

proc & method

#curry

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
end
method(: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
end

#===

strict equality operator

def 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'
end
case animal
when method(:poisoning?), 'spider'
  'run away'
when method(:cute?)
  'give it a home'
end
if method(:poisoning?) === animal || \
    'spider' === animal
  'run away'
else method(:cute?) === animal
  'give it a home'
end
method(:poisoning?) === animal
method(:poisoning?).call animal

#>>

right shift operator (since 2.6)

def 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

#<<

left shift operator (since 2.6)

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

In Short

&method

8.times.each &method(:puts)

#

  • #curry
  • #===
  • #>>
  • #<<

Question?

Made with Slides.com