“闭包”这个词看起来很熟悉，在编程书中经常看见，但是我好像从来没真正理解这是个啥意思。今天查了很多资料，决定弄清楚这个词的含义。

In programming languages, a closure (also lexical closure or function closure) is a function or reference to a function together with a referencing environment—a table storing a reference to each of the non-local variables (also called free variables or upvalues) of that function.[1] A closure—unlike a plain function pointer—allows a function to access those non-local variables even when invoked outside its immediate lexical scope.

def compare_property(shape1, shape2, property)
attr1 = property.call(shape1)
attr2 = property.call(shape2)
if attr1 > attr2
1
elsif attr1 == attr2
0
else
-1
end
end

def compare
property = lambda{|shape| shape[:area]}

shape1 = {:perimeter => 12, :area => 9}
shape2 = {:perimeter => 18, :area => 8}
compare_property(shape1, shape2, property)
end

puts compare()


In general, an operation for combining data objects satisfies the closure property if the results of combining things with that operation can themselves be combined using the same operation.