xyzzyb + programming + style   4

The Procedure/Function Block Convention in Ruby
Ruby lets you enclose blocks in either {...} or do...end delimiters. Which you choose is a matter of style.

There are two conventions that I know of for deciding which form to use. The one I see people using most often these days is the “line count” convention: curly brackets for one-liners, do...end for multiline statements.

The alternative—and the convention I’ve been using for a while—is based on the semantic intent of the block, rather than on its length. It can be stated like this:

- Use curly brackets for functional blocks, where the primary purpose of the block is to return a value.
- Use do...end for procedural blocks, where the primary purpose of the block is its side-effects. That is, the block is intended to change the state of the system in some way or to perform output.
ruby  programming  style  brackets  do  end 
december 2011 by xyzzyb

Copy this bookmark: