From: Hugh Sasse Staff Elec Eng
Date: 2000-10-19T02:06:25+09:00
Subject: [ruby-talk:5673] Re: local variables (nested, in-block, parameters, etc.)
On Thu, 19 Oct 2000, ts wrote:
> >>>>> "H" == Hugh Sasse Staff Elec Eng writes:
>
> H> But the manual page states they are the same: "for is the syntax sugar
> H> for:" etc. That was my point. If they are not equivalent, but they are
> H> so near that the author of the manual could confuse the two,
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> I don't think that the author of the manual can confuse the two :-)
Then 'for' simply isn't syntactic sugar for the braces or for the
do...end. It does a similar job, but does it differently. The author
may not be confused, but confuses the issue for the reader by putting
things like this. No disrespect intended: creating clear documentation
is non-trivial!
>
> H> if 'do' and 'end' do not introduce new scope, whereas the braces do. But
> H> actually trying this:
>
> 'do' and 'end' introduce new scope, except when used with 'while',
> 'for', ... :-)
Oh! These are treated differently because the do word is optional
there. OK. I think I've got there. I better retract that patch then!
I would suggest a new patch replacing my earlier one:
--- syntax.html.orig Thu Jun 22 16:18:36 2000
+++ syntax.html Wed Oct 18 17:59:54 2000
@@ -1555,8 +1555,14 @@
- (expr).each `{' `|' lhs..`|' expr.. `}'
+ lhs = nil
+ (expr).each `do' `|' lhs..`|' expr.. `end'
+
+but without creating local scope for expr.
+Note: lhs will be defined when the
+for loop finishes.
+
>
> H> which seems to be the same as braces to me. Hmmm. I thought I
> H> understood the difference between braces and do...end.
>
> There is a difference between 1.4 and 1.6
>
> pigeon% cat b.rb
> #!/usr/bin/ruby
> def b
> yield 14
> end
> def foobar(a, b)
> p "#{a} -- #{b}"
> end
> a = 12
> foobar a, b {|i| i}
> foobar a, b do |i|
> i
> end
> pigeon%
>
> pigeon% b.rb
> "12 -- 14"
> "12 -- 14"
> pigeon%
>
> pigeon% ./ruby b.rb
> "12 -- 14"
> b.rb:3:in `b': yield called out of iterator (LocalJumpError)
> from b.rb:10
> pigeon%
>
> pigeon% ./ruby -v
> ruby 1.4.6 (2000-08-16) [i686-linux]
> pigeon%
>
So that rule about precedence has gone in 1.6?
>
> Guy Decoux
>
>
Hugh
hgs@dmu.ac.uk