[Prev][Next][Index][Thread]

Re: Closures



In article <uvqlmkmyfr4.fsf@oroboros.ai.mit.edu>, Rick T wrote:
>
>   Does anyone know if Ruby implement proper lexical closures in this sense?

Yes.  The following snippet demonstrates this (I hope...):

def closure
  val = 0
  return proc {
    val += 1
    val
  }
end

block = closure

puts block.call
puts block.call
puts block.call
puts block.call
puts block.call

This prints:
1
2
3
4
5

You can do the same in Perl afaik.

-- 
Mark Hulme-Jones <mjones@frottage.org>



References: