"Hal E. Fulton" wrote:

> >
> > I would prefer that the code in example 1, if in a real program, use a
> different local
> > variable, but I did understand with little effort that it was intended to
> be a local
> > variable.  Any other choice would have increased my surprise.  Several
> languages that use
> > an approximately equivalent form also require that tie loop control
> variable be local.  I
> > think that the languages that don't make this choice are in the minority.
> And deservedly
> > so.
>
> Does this surprise you, then?
>
> index = 345
>
> for index in 1..5
>   print "#{index}\n"
> end
>
> print index   # Prints 5
>
> Hal
>
> > -- (c) Charles Hixson
> > --  Addition of advertisements or hyperlinks to products specifically
> prohibited
> >
> >

Ogh.  Yes!  OTOH, I know that I don't really understand the evaluated symbol
usage.  Still, I would expect it to print:
1
2
3
4
5
345

I don't know what it does print.  OK, I investigated.  I guess that <i>for</i>
isn't establishing a scope.  This does seem a bit inconsistent.  But under ruby
1.6.0 on Win95 it produced 5 as the (final) result.  (Well, nil in eval.rb, but
that's a real quibble.)

You are definitely right.  I expected the loop variable to evaporate, and
instead it staked permanent residence in whatever the current scope was, and I
presume that it would do the same in a real program rather than just in an
eval.rb test.  Definitely something to watch out for!
-- (c) Charles Hixson
--  Addition of advertisements or hyperlinks to products specifically
prohibited