----- Original Message -----
From: "Gavin Sinclair" <gsinclair / soyabean.com.au>
To: "ruby-talk ML" <ruby-talk / ruby-lang.org>
Sent: Thursday, October 03, 2002 8:58 PM
Subject: Specifying local and external block parameters (that old chestnut)


> I think it makes sense to explicitly mark *external* variables, as
block-local
> variables (parameters, in fact) would be the norm.
>
> Thus, if 'a' and 'c' are regular parameters and 'b' is an external
variable,
> then one could write
>
>   collection.each do |a,:b,c|
>     ...
>   end
>
> or
>
>   collection.each do |a,b,c|
>     external b
>     ...
>   end
>
> I'm not sure which of the above I prefer.  One should optimise for the
common
> case.

One problem is that it's not just a simple variable. It
can be anything that responds to assignment. For example,
an accessor:

  0.upto(99) {|frame.scroll| sleep 0.1}

> I'd like to put a question to the audience :)  Does anyone actually have a
need
> to use non-local block parameters?  Ever?

The above is an example.

Also what if I want the last value(s) of a loop?

  x,i = nil
  myobj.each_with_index do |x,i|
    # process...
    break if some_condition
    # more processing...
  end

  last_obj = x
  last_index = i

Yes, I *could* save them off in different variables
right before I did the break:

  y,j = nil
  myobj.each do |x,i|
   #...
   if some_condition
     y,j = x,i
     break
   end
   #...
  end

  last_obj, last_index = y,j

Two more variables and much ugliness. No, thanks.

Hal