The two syntaxes for creating a code block:

A code block can be passed to a method as an optional block argument. Many built-in methods have such arguments:

Parameter-passing a block to be a [[Closure (computer science)|closure]]:

Creating an [[anonymous function]]:

Returning [[Closure (computer science)|closures]] from a method:

Yielding the flow of program control to a block which was provided at calling time:

Iterating over enumerations and arrays using blocks:

A method such as codice_2 can accept both a parameter and a block. The codice_2 method iterates over each member of a list, performing some function on it while retaining an aggregate. This is analogous to the codice_4 function in [[functional programming languages]]. For example:

On the first pass, the block receives 10 (the argument to inject) as codice_5, and 1 (the first element of the array) as codice_6. This returns 11, which then becomes codice_5 on the next pass. It is added to 3 to get 14, which is then added to 5 on the third pass, to finally return 19.

Using an enumeration and a block to square the numbers 1 to 10 (using a "range"):

Or invoke a method on each item (codice_8 is a synonym for codice_9):

Blocks and iterators
Exceptions
ଉଦାହରଣ
ବାହାର ଲିଙ୍କ