Hi, you've reached the website of Mark Menard, Freemason, developer, businessman, photographer, motorcyclist and all around nice guy.
The main joy in my life is Sylva, my loving partner and friend. (You will see plenty of pictures of her. Also check out our site.) My professonal career is running a consulting firm concentrating on business automation and custom software. I enjoy software development, motorcycles, photography, dancing, freemasonry and travel.
Here you will find galleries of my latest photos, thoughts on software development, freemasonry, the occassional politics, and other things. Enjoy your time here.
Mark
An ongoing photographic study of Freemasonry.
To contact Mark send an email to mark@mjm.net. You will receive an auto response from my mail server to verify you're not a spammer.
© Mark Menard 2002-2007
Partial answer
In the first example that you wrote you are closing over i, which we must remember is a reference. Each time through we are closing over the reference that is i, which is what is referred to in the closure when it's invoked.
Just as an FYI, Ruby's behavior is the same as Groovy's.
I'm still working on the second example.
To get the behavior you're looking for try this:
def myArray = [] 0.upto(2) { i -> myArray.add ( {println "Current i is: ${i}"} ) } myArray.each { it() }In this example the output from the closure is the value of i when the closure is created in the iteration because each i is a different reference.
Mark