Closures
When an expression or a block of code is passed as a parameter to a function or method then the piece of code that gets passed is called a closure in programming language terms. Closure is not something new and has been supported in programming languages like Smalltalk, Lisp, Python etc. A closure is like nameless functions within functions. Closures are supported in Ruby and are called blocks. A simple closure example in Ruby would be,
5.times {puts “Hello World”}
This prints “Hello World” five times. This code is the equivalent of the below code,
for i in 1..5
puts “Hello World”
end
Complex closures can be tricky to understand but once a developer gets the point how it works it could simplify or complex depending on the problem in hand and where it is applied. Some programming languages like Java don’t support closures yet. Is closures really handy or inconvenience? Read this article “Crossing borders: Closures” for more insights.












