[ next ] [ prev ] [ contents ] [ skip to Unit Testing ] Using Ruby

Understanding Blocks

block = proc { |x| puts "Called with #{x}" }

block.call(1)
block.call(2)

block[222]

Output

Called with 1
Called with 2
Called with 222

  • Procs are objects that can be called.

  • Sending the call method to the proc object executes the code block.

  • The [] operator is a synonym for call.


[ next ] [ prev ] [ contents ] [ skip to Unit Testing ] Copyright 2003 by Jim Weirich.
Some Rights Reserved