Understanding How Ruby Blocks Work
I’ve been preparing the curriculum for a class about Ruby blocks. The word “block” just means a block of code that gets passed to a method right after other parameters. When a block is sent to a method, the code within it is then available to the method. The method can call the block of code any time it wants to using the “yield” keyword. To sum it up, the two important pieces of blocks, are:
- the code block
- the yield keyword
Look below…first I write my method, dos_veces. When I call dos_veces, I add some code to the end of it between curly braces {…}. That code gets executed by the method whenever I typed “yield” within my method definition.


I’ve found a few extremely helpful websites including: Eli Bendersky’s blog post about blocks procs and methods, Robert Sosinski’s blog post about blocks, procs and lamdas, Ruby Learning: Blocks Tutorial, and Jamis Buck’s blocks rock post.
Posted by Liah on Sunday, January 17, 2010