Begin/rescue/end versus do/end

Begin/rescue/end versus do/end

Post by Sam McCal » Thu, 19 Aug 2004 17:06:00


These don't seem orthogonal. It feels like I should be able to do
obj.meth do |x|
#...
rescue SomeException => ex
#...
end

and that either begin should be something like a Kernel method:

def begin
yield
end

(although maybe with optional "do"), or blocks could be standalone
expressions if not part of a method call (this doesn't _seem_ to cause
ambiguity, but someone cleverer could probably find some).
So I could do

do
#...
rescue SomeException => ex
#...
end

which behaves just like "begin", and I wouldn't have to use blocks in
some places (iterator methods) and begin/end in others (builtin control
structures):

do x.foo; x.bar; end if x.baz? , or
{ x.foo; x.bar } if x.baz?
instead of
begin x.foo; x.bar; end if x.baz?

The syntax difference between iterators and builtin control structures
(need the do), and the fact that one checks for require blocks and one
doesn't, still rub me the wrong way a bit:
while(foo) ... end
but
loop do ... end
^^
I don't want to know if it's a builtin or an iterator.

What do people think? I hope I'm completely wrong, and it makes perfect
sense... and someone can explain it :-)

Sam
 
 
 

Begin/rescue/end versus do/end

Post by Robert Kle » Thu, 19 Aug 2004 19:33:11


"Sam McCall" < XXXX@XXXXX.COM > schrieb im Newsbeitrag


Two things come to mind:

- AFAIR there was a discussion about this some time ago here

- this issue might be related to block scoping rules, currently you can
introduce a local in a block but AFAIK begin-end does not introduce a
separate scope (note also, that block scoping will change in Ruby 2)

$ ruby -e 'begin; foo="foo"; end;puts foo'
foo

Robert@Babelfish2 ~
$ ruby -e 'def t;yield;end; t { foo="foo" } ; puts foo'
-e:1: undefined local variable or method `foo' for main:Object (NameError)


Kind regards

robert

 
 
 

Begin/rescue/end versus do/end

Post by Sam McCal » Thu, 19 Aug 2004 22:11:32


Thanks, I'll have a look.

AFAICS, the changes mean this difference goes away, only block
parameters are scoped to the block.

Sam