I was having an interesting discussion about a few of these terms, and I got a little confused over the exact semantics on these. So here’s my quickie summary.
Blocks: Wiki) A group of declarations and statements meant to encapsulate variables into a scope. Does not necessarily return or expect arguments.
Closures: Wiki) A first-class function whose variables are contained within a scope. Usually created as an anonymous function object.
Procs in Ruby: Behave very much like blocks – they do not check argument counts, for example. Also calling a Proc with a return statement returns from the caller’s scope, since a proc executes in the same scope as the caller.
Lambdas in Ruby: Behave more like named methods. Checks argument counts, and returns values from the contained block when called, not from the caller scope.
Quick example stolen from Wikipedia):
def foo f = Proc.new { return "return from foo from inside proc" } f.call # control leaves foo here return "return from foo" end def bar f = lambda { return "return from lambda" } f.call # control does not leave bar here return "return from bar" end puts foo # prints "return from foo from inside proc" puts bar # prints "return from bar"
See this StackOverflow for some examples of Procs and Lambdas in ruby.