From: Paul Lutus Date: 2006-09-11T14:10:24+09:00 Subject: Re: Struggling with Blocks Ken Bloom wrote: / ... > Let me try a direction in answering this question. I don't know anything > about the internals of Ruby to know whether this is right or not, but > perhaps someone can confirm. > > do..end and {...} blocks are lexical closures that remember their > surrounding binding. This requires setup and teardown code for dealing > with bindings that could otherwise go out of scope. > > begin...end can't be passed around like do...end, and so it doesn't need > the same kind of setup and teardown code for dealing with bindings. > > Different syntax is needed because the ruby interpreter isn't smart enough > to know which situation is which. > > Is this close to correct? It's reasonable. I can put a "do ... end" block in a Proc object (because that's what it is) but I can't do this with begin ... end (unless the begin ... end block is nested in a do ... end block). So they differ that way. This may not be a particularly persuasive argument for or against rescue within do ... end, because I can put a rescue clause in a begin ... end block that is nested in a do ... end block. Like so: ------------------------------------------------- #! /usr/bin/ruby p = Proc.new { |s| begin puts s y = x/0 rescue puts "Error!" end } [ 'a' , 'b', 'c' ].each { |s| p.call(s) } Output: a Error! b Error! c Error! -- Paul Lutus http://www.arachnoid.com