Main Content

Passing code to procedures and yield in Ruby

Archive - Originally posted on "The Horse's Mouth" - 2010-02-02 07:42:53 - Graham Ellis

If you want to pass a block of code into a Ruby procedure, you can do so by passing in the code to a variable who's name you start with an & in the procedure definition; you can then run that code using the .call method on the received object. In some ways this is an indirect reference - in Ruby you can's simply assign code to a variable.

You can also pass a curly brace defined block / closure into a procedure by specifying it as the piece of code that the procedure should jump to when it encounters a yield.

There are examples of both of these syntaxes at [this location].