Ruby - Statement yield

Introduction

Consider the following code:

def aMethod
   yield
end

Here, the aMethod doesn't really have any code of its own.

It expects to receive a block, and the yield keyword causes the block to execute.

This is how to pass a block to it:

aMethod{ puts( "Good morning" ) }

The block is not passed as a named argument.

It would be an error to try to pass the block between parentheses, like this:

aMethod(  { puts( "Good morning" ) }  )     # This won't work!

You simply put the block right next to the method to which you are passing it.

That method receives the block without having to declare a named parameter for it, and it calls the block with yield.