Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In the context of the Ruby programming language, what is a closure and when do you use one? What are the uses for it in Rails?

share|improve this question
up vote 5 down vote accepted

Try this link: Understanding Ruby Closures. Another one: Understanding Ruby Blocks, Procs, and Lambdas. In a nutshell, a closure is a function plus a context in which the function is called. The context contains state information that the function can access, but which isn't part of the function itself. Ruby has several different kinds of closures: blocks, procs, and lambdas.

Also (from Michael):

share|improve this answer
This is little more than a link-only answer. – raptortech97 Dec 20 '14 at 2:52
@raptortech97 It's actually "a little more" than a link only answer, where the "little more" is a concise explanation of what a closure is and how that relates to Ruby. – Caleb Dec 20 '14 at 22:08

protected by gnat Apr 14 '15 at 19:26

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.