You and Mr. Gross helped me understand what a closure is. Thanks a lot! By the way, where does the name - closure come from? It's not a very intuitive name, IMO. Sam "Bill Atkins" <batkins57 / gmail.com> wrote in message news:66b7e34b041019113143477b7a / mail.gmail.com... > Methods aren't closures in Ruby - only blocks are. In that example, > the makeCounter method is returning a lambda, which is a block (and > thus a closure). A closure is (from what I understand) just a block > that remembers its context. Consider this: > > a = 34 > b = proc { a * 2 } > > # in a different scope > b.call # = > 68 > > Even though a has gone out of scope by the time b is actually called, > Ruby saves the value of a with b so that when you call it, b can > return the proper value. In other words, a closure is just a block > that refers to its environment. > > Bill > > On Wed, 20 Oct 2004 03:04:20 +0900, Sam Sungshik Kong > <ssk / chol.nospam.net> wrote: > > Hi, group! > > > > I am trying to understand what exactly a closure is. > > I'm kinda familiar with C#. > > In C# 2.0 (the next version that's coming out), there's a anonymous > > delegate. > > People say that it's a closure (See > > http://martinfowler.com/bliki/Closures.html). > > They even compare it with Ruby codes saying that closure is block. > > Is it true? > > > > When I saw the closure example in Ruby, it was not just a block. > > > > Here's a typical example of Ruby closure. > > > > <snip> > > def makeCounter > > var = 0 > > lambda do > > var +=1 > > end > > end > > > > c1 = makeCounter > > c1.call > > c1.call > > c1.call > > > > c2 = makeCounter > > > > puts "c1 = #{c1.call}, c2 = #{c2.call}" > > </snip> > > > > Here is what the above page(http://martinfowler.com/bliki/Closures.html) > > says... > > > > <snip> > > Closures have been around for a long time. I ran into them properly for the > > first time in Smalltalk where they're called Blocks. Lisp uses them heavily. > > They're also present in the Ruby scripting language - and are a major reason > > why many rubyists like using Ruby for scripting. > > > > Essentially a closure is a block of code that can be passed as an argument > > to a function call. > > > > .... > > > > In a language that has Closures, in this case Ruby, I'd write this. > > > > def managers(emps) > > return emps.select {|e| e.isManager} > > end > > </snip> > > > > This is just a block, right? > > Is it also a closure? > > > > I'm confused. > > Can somebody enlighten me? > > > > > > Sam > > > > > >