Proc literal

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

  1. def some_proc(&block : Int32 -> Int32)
  2. block
  3. end
  4. x = 0
  5. proc = ->(i : Int32) { x += i }
  6. proc = some_proc(&proc)
  7. proc.call(1) # => 1
  8. proc.call(10) # => 11
  9. x # => 11

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

  1. def add(x, y)
  2. x + y
  3. end
  4. adder = ->add(Int32, Int32)
  5. adder.call(1, 2) # => 3