Ruby: The & operator

After reading, you will know why and how runners.each(&:run) works. Here some tidbits:

& can be quite confusing because it has a different meaning depending on the context in which it's used.

&object is evaluated in the following way:

  • if object is a block, it converts the block into a simple proc.
  • if object is a Proc, it converts the object into a block while preserving the lambda? status of the object.
  • if object is not a Proc, it first calls #to_proc on the object and then converts it into a block.
Dominik Schöler Over 10 years ago