Hi --

On Mon, 12 Jan 2009, Mike Stephens wrote:

> The good news is that the rules are pretty
>> well-tailored to the actual programming needs you're like to have,
>> which include wanting to write closures (so that the block captures
>> the local variables from the scope of its creation)
>
> Now accept that I'm talking in the abstract here (I've done very little
> Ruby coding), I would have thought that if you want to access data from
> another piece of code, you should call a method unless that target has
> been specifically declared as public or global. Ruby of course allows
> you to directly access data declared as private. These are examples of
> why I think Ruby is highly sophisticated and therefore has a potentially
> high barrier-to-entry. and in turn why I suspect the answer for many
> people is to limit yourself to a simple sub-set.

Closures aren't about breaking privacy encapsulation. The point of
preserving the local variables in a closure is so that you can define
functions in one place and use them in another, and they can do what
they did at the point where they were defined:

   def give_me_a_counter
     n = 0
     lambda { n += 1 }
   end

   c = give_me_a_counter
   puts c.call          # 1
   puts c.call          # 2
   puts c.call          # 3
   d = give_me_a_counter
   puts d.call          # 1
   puts c.call          # 4

The variable n is local to give_me_a_counter, but the lambda it
returns isn't -- and the lambda happens to use a local variable. This
is not related to the kinds of privacy things I think you're talking
about (like instance_variable_get).


David

-- 
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!