From: Hal Fulton Date: 2006-09-11T14:39:28+09:00 Subject: Re: Struggling with Blocks Ken Bloom wrote: > > 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? > Disclaimer: I don't know the internals of Ruby either. This is only guesswork on my part, and stands a large chance of being wrong. I believe what you're saying is close to correct. You're correct in saying that rescue requires some extra setup and teardown. That seems intuitively obvious to me. My guess is that "begin" is like an extra hint to the interpreter, a foreshadowing if you will, that there is probably going to be a rescue (and thus some setup/teardown). Note that def/end and begin/end are much rarer in code than do/end and {}. So if we start allowing rescue on do/end and {}, we expose the interpreter to a lot of guessing or tentatively holding onto information or "looking ahead." Stuff like: Do the setup, but only if there's a rescue forthcoming. Or do the setup, but throw it away if we never see a rescue. Hal