> def proc_with_enclosing_scope
>   name = "Ruby"
>   lambda { puts name }
> end

This example relies on the fact that Ruby methods implicitly return the 
value of the last expression evaluated. So perhaps you would find it 
clearer as:

def proc_with_enclosing_scope
  name = "Ruby"
  return lambda { puts name }
end

or even:

def proc_with_enclosing_scope
  name = "Ruby"
  my_proc = lambda { puts name }
  return my_proc
end

Now you can see that:

> the_proc = proc_with_enclosing_scope
> the_proc.call

just assigns the returned value (which happens to be a Proc object) to 
the_proc, and then invokes the 'call' method on it.

This example isn't much different to

  return lambda { puts "Ruby" }

Where it gets more interesting is where you can modify the values inside 
the closure:

  def counter
    count = 0
    lambda { count += 1 }
  end

  c1 = counter
  c2 = counter
  puts c1.call
  puts c1.call
  puts c1.call
  puts c2.call
  puts c2.call
  puts c1.call
  puts c2.call

This demonstrates that the Proc objects c1 and c2 have their own 
independent 'count' variables. They behave almost like instance 
variables.

Regards,

Brian.
-- 
Posted via http://www.ruby-forum.com/.