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

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

def begin

(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

rescue SomeException => ex

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

do;; end if x.baz? , or
{; } if x.baz?
instead of
begin;; 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
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 :-)


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'

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



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.