Proc literal

A captured block is the same as declaring a Proc literal and passing it to the method.

def some_proc(&block : Int32 -> Int32)
  block
end

x = 0
proc = ->(i : Int32) { x += i }
proc = some_proc(&proc)
proc.call(1)  # => 1
proc.call(10) # => 11
x             # => 11

As explained in the proc literals section, a Proc can also be created from existing methods:

def add(x, y)
  x + y
end

adder = ->add(Int32, Int32)
adder.call(1, 2) # => 3

results matching ""

    No results matching ""