This is the fourth part of my Road to Ruby enlightenment. It started a long time ago with tutorials and first steps.

Road To Ruby Enlightenment
1. tutorials and first steps
2. talk, quiz and cookbook
3. red and gray
4. lambda, proc etc.

Blocks and closures are one of the reasons why you can do magic in Ruby. Some time ago I read Eli’s Understanding Ruby blocks, Procs and methods which does a great job of explainig the whole topic. As long as it doesn’t involve some high-level ruby magic (like Why’s A Block Costume) I felt like I understood most of the code using closure-like constructs.

When I used closures in my own code, every once in a while something would not work the way I expected it to. I always found a workaround and never spend much time worrying about these oddities.

This morning (via Ruby Inside) I found closures-in-ruby.rb by Paul Cantrell. Seems like he had the same problems but instead of ignoring them he played around and documented the things he found out. The article highlights some suprises and differences between the 7 ways of creating closure-like thingies in ruby. Highly recommended reading!


Any comments? Or questions? Just leave a Reply: