f



Begin/rescue/end versus do/end

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
0
8/18/2004 8:06:00 AM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

2 Replies
388 Views

Similar Articles

[PageSpeed] 6

"Sam McCall" <tunah.usenet@tunah.net> schrieb im Newsbeitrag
news:1092816068.68668@drone1-svc-skyt.qsi.net.nz...
> 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 :-)

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

0
bob.news (3807)
8/18/2004 10:33:11 AM
Robert Klemme wrote:

> 
> Two things come to mind:
> 
>  - AFAIR there was a discussion about this some time ago here
Thanks, I'll have a look.

>  - 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)
AFAICS, the changes mean this difference goes away, only block 
parameters are scoped to the block.

Sam
0
8/18/2004 1:11:32 PM
Reply: