From: Phlip Date: 2004-10-20T03:44:19+09:00 Subject: Re: Question about closure Sam Sungshik Kong wrote: > Can somebody enlighten me? In addition to the other (unread) answers: X = 42 oval.bind('Button-1') { |event| doc(event) puts x.inspect() } Ruby statements between { and } (or begin and end) form an object, called a block, which passes into any method on its left. That method can evaluate the statements inside the block immediately, and can store the block as an object, called a Proc, so something can call it later. The block links to instances of variables seen from functions around it, such as our x, even after such functions return. Each instance of the block links to a parallel instance of each variable, so such variables persist until the block itself destroys. Languages such as Perl and Smalltalk also use these "block closures". They are more than a convenience: Variables must always have the narrowest scope possible, and variables linked to a block, such as x, have the narrowest conceivable scope over an indefinite lifespan. -- Phlip http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces