Ruby blocks, Procs and methods

Ruby provides the programmer with a set of very powerful features borrowed from the domain of functional programming, namely closures, high–order functions and first–class functions [1]. These features are implemented in Ruby by means of code blocks, Proc objects and methods (that are also objects) – concepts that are closely related and yet differ in subtle ways. In fact I found myself quite confused about this topic, having a difficulty to understand the difference between blocks, procs and methods and unsure about the best practices of using them. Additionally, having some background in Lisp and years of Perl experience, I was unsure of how the Ruby concepts map to similar idioms from other programming languages, like Lisp’s functions and Perl’s subroutines. Sifting through hundreds of newsgroup posts, I saw that I’m not the only one with this problem, and in fact quite a lot of ”Ruby Nubies” struggle with the same ideas.

In this article I lay out my understanding of this facet of Ruby, which comes as a result of extensive research of Ruby books, documentation and comp.lang.ruby, in sincere hope that other people will find it useful as well.

Posted by Administrator on Wednesday, November 01, 2006

digg delicious technorati blinklist furl reddit