Summary about blocks in ruby

1 blocks are just one way ruby implements closures. Proc and lambdas are others and less common.

2 closures drag their surrounding context/environment around, and this is at the core of how variable scope works.

3 blocks are great for pushing some decisions to method invocation time.

4 blocks are great for wrapping logic, where you need to perform some before/after actions.

5 we can write our own methods that take a block with the yield keyword.

6 when we yield, we can also pass arguments to the block.

7 when we yield, we have to be aware of the block’s return value.

8 once we understand blocks, we can re-implement many of the common methods in the ruby standard library in our own classes.

9 the Symbol#to_proc is a nice shortcut when working with collections.

Show your support

Clapping shows how much you appreciated ruby Spike’s story.