Closures

Closures (aka Blocks) are essentially blocks of code that can be passed as arguments to a function or method. Unlike method pointers, anonymous classes, and other similar things, closures have the unique ability to be able to see the value of variables in the same scope they were created in. For example, if you declare a variable on one line and a closure on the next, the closure will have visibility of the variable when the closure executes, assuming it is passed into the closure.

Martin Fowler has a great example of this. To pull only certain items out of a collection in Java you’d have to do something like this:

 

public static List<Item> getItemsWeCareAbout(List<Item> orig) {

	List<Item> toReturn = new ArrayList();
	int val = 230;

	for (Item item : orig) {
		if (item.meetsConditionWeCareAbout(val)) {
			toReturn.add(item);
		}
	}

	return toReturn;
}

However, the same code in Ruby can be done like this:

 

def items_we_care_about(orig)   val = 230   return orig.select {

	|i| i.meetsConditionWeCareAbout val

} end

The Ruby version utilizes closures in which we can pass a block of code into the select method. The actual closure is the code within the curly braces ({..}). This code simply says that each item (|i|) that returns true from the “meets conditions we care about” method should be included in the return collection. Also note that the variable declared in the method is passed into the closure, meaning that the closure has visibility into the variable each time it is executed, not just when it was defined.