From: Robert Klemme Date: 2006-10-28T22:25:12+09:00 Subject: Re: What are closures, continuations? "Une B�vue" wrote: > Justin Collins wrote: > >> Closures are tricky, but you can think of them as blocks of code, or >> a way of making code a first-class object. Or as anonymous functions. > > could we say they work as anonymous class in java ??? There are some similarities but also restrictions. For example you can only reference final variables from the current environment which means you cannot change them. That in turn is typically possible with closures: irb(main):001:0> def counter(init=0) lambda {|*x| init += x[0]||1} end => nil irb(main):002:0> c = counter => # irb(main):003:0> c[] => 1 irb(main):004:0> c[] => 2 irb(main):005:0> c[] => 3 irb(main):006:0> c[] => 4 irb(main):007:0> c[10] => 14 In a way every class is a closure as its instances carry around some state they can always access. But the term "closure" is generally /not/ used for this - this is called "encapsulation". Kind regards robert