You want to pass a function around like a variable, and while doing so, you want that function to be able to refer to one or more fields that were in the same scope as the function when it was declared.
In his excellent article, Closures in Ruby, Paul Cantrell states
A closure is a block of code which meets three criteria
He defines the criteria as follows:
- The block of code can be passed around as a value, and
- It can be executed on demand by anyone who has that value, at which time
- It can refer to variables from the context in which it was created (i.e., it is closed with respect to variable access, in the mathematical sense of the word “closed”).
Scala Cookbook, give a more graphic metaphor:
I like to think of a closure as being like
quantum entanglement, which Ein‐ stein referred to as “a spooky action at a distance.” Just as quantum entanglement begins with two elements that are together and then separated—but somehow remain aware of each other—a closure begins with a function and a variable defined in the same scope, which are then separated from each other. When the function is executed at some other point in space (scope) and time, it is magically still aware of the variable it referenced in their earlier time together, and even picks up any changes to that variable.