On Tue, Aug 01, 2006 at 07:05:31AM +0900, Logan Capaldo wrote:
> 
> On Jul 31, 2006, at 5:13 PM, Chad Perrin wrote:
> 
> >On Tue, Aug 01, 2006 at 04:57:48AM +0900, Logan Capaldo wrote:
> >>
> >>All these examples are lexical scoping. Ruby doesn't really have
> >>dynamic scoping although you can sort of abuse instance variables to
> >>achieve similar effects.
> >>
> >>The difference is that blocks are closures, where def, class, and
> >>module aren't.
> >
> >Wait . . . you mean that *all blocks* are automagically lexical
> >closures, as though declared lexical variables within them have  
> >gone out
> >of scope?  I imagine I'm probably misunderstanding you, but if not,
> >that's a pretty nifty bit of trickery.
> >
> I'm not sure I understand your question. All blocks (by blocks I mean  
> do / end and { } ) are (lexically scoped) closures.

I'll use a Perl example:

sub foo {
  my $bar = 1;
  return sub { print ++$bar };
}

my $baz = foo();

Voila.  $baz contains a lexical closure.  This is the case because the
return value from foo() was lexically "closed" by virtue of $bar going
out of scope, but its value still being accessible via the coderef
returned from foo() and assigned to $baz.

-- 
CCD 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