On Tue, 2004-10-19 at 20:44, Sam Sungshik Kong wrote: > 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. A closure is a combination of an anonymous function and the lexical environment it was created in. You could say it's a closure around this lexical environment (=all the current bindings of local variables/all the nested environments) at that moment. If you have a method like def foo; end its lexical environment does only exist while executing the method. But you can also return the environment if you for example use def foo;binding;end # => nil foo # => #<Binding:0x402aed74> foo # => #<Binding:0x402adf8c> That opens a lot of possibilities, because you can still hold a reference to the environment after the method call was executed. If you have real closures you also have object oriented programming. A small example is this stack implementation: def make_stack data = [] lambda do |id,*args| case id when :push then data.push *args when :pop then data.pop when :top then data.last end end end # => nil s1 = make_stack # => #<Proc:0x402af044@(irb):3> s2 = make_stack # => #<Proc:0x402af044@(irb):3> s1[:push, 1, 2, 3] # => [1, 2, 3] s2[:push, 'a', 'b', 'c'] # => ["a", "b", "c"] s1[:top] # => 3 s2[:top] # => "c" s2[:pop] # => "c" s1[:top] # => 3 s2[:top] # => "b" Note that s1 and s2 are two different stack objects, because they access a different environment and thus a different data variable. You might be interested in reading the wizard book http://mitpress.mit.edu/sicp/full-text/book/book.html, especially chapter 4, "The Metacircular Evaluator". -- _="puts'_='+_.inspect+';'+_";puts'_='+_.inspect+';'+_