From: Chad Perrin Date: 2007-04-09T04:25:58+09:00 Subject: Re: code blocks and methods On Mon, Apr 09, 2007 at 04:11:56AM +0900, Chad Perrin wrote: > > As botp pointed out, there really isn't much of a difference between the > two. The major difference is how they are used. Essentially, a block > is a method that has no name, is not specifically associated with any > particular object (as far as I'm aware), and can be passed around by > itself as an object (such as "stored in" a variable). One other thing: To understand how blocks differ from methods, it might also help to read about similar constructs in other languages. For instance, the difference is analogous (but not identical, because we're talking about methods rather than functions) to the difference between named functions and lambdas in several languages. Another place to look might be the discussion of named subroutines, callbacks, and closures in the O'Reilly book Learning Perl (aka The Llama Book) by Randal Schwartz et al. In essence, a block in Ruby is something like an anonymous subroutine in Perl, a proc like a callback, and a proc formed with lexical context is a closure. I'm sure someone will disagree with my definition of closure, just based on the common assertion that all blocks are closures in Ruby, period, so read up on closures and decide for yourself. It's helpful in doing so to think about where the name "closure" originates in this context. -- CD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] "The measure on a man's real character is what he would do if he knew he would never be found out." - Thomas McCauley