From: Eric Hodel Date: 2006-05-19T06:44:19+09:00 Subject: Re: Rescue clauses on do/end blocks? --Apple-Mail-9-30243354 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed On May 18, 2006, at 11:22 AM, Jim Weirich wrote: > Eric Hodel wrote: >> Setting up and tearing down an exception handler for every block >> invocation is going to be expensive. > > Couldn't the setup/teardown be done only if a rescue/ensure clause was > actually present? It also seems to me that this would mean the the > setup/teardown would have to be in the block itself, not in the > calling > method. I forgot that the parser will just take care of it. The patch was easy, applies to a recentish copy of the 1.8 branch: --Apple-Mail-9-30243354 Content-Transfer-Encoding: 7bit Content-Type: application/octet-stream; x-unix-mode=0644; name="do-rescue.patch" Content-Disposition: attachment; filename=do-rescue.patch Index: parse.y =================================================================== RCS file: /src/ruby/parse.y,v retrieving revision 1.307.2.34 diff -u -r1.307.2.34 parse.y --- parse.y 19 Apr 2006 04:57:55 -0000 1.307.2.34 +++ parse.y 18 May 2006 21:36:19 -0000 @@ -1782,7 +1782,7 @@ $1 = ruby_sourceline; } opt_block_var {$$ = ruby_dyna_vars;} - compstmt + bodystmt kEND { $$ = NEW_ITER($3, 0, dyna_init($5, $4)); @@ -1845,7 +1845,7 @@ $1 = ruby_sourceline; } opt_block_var {$$ = ruby_dyna_vars;} - compstmt '}' + bodystmt '}' { $$ = NEW_ITER($3, 0, dyna_init($5, $4)); nd_set_line($$, $1); @@ -1857,7 +1857,7 @@ $1 = ruby_sourceline; } opt_block_var {$$ = ruby_dyna_vars;} - compstmt kEND + bodystmt kEND { $$ = NEW_ITER($3, 0, dyna_init($5, $4)); nd_set_line($$, $1); --Apple-Mail-9-30243354 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed (I'm still not sold on it, it was just really easy.) > Thus making it hard to rescue exceptions from the calling method > (as some have suggested). I don't believe it is possible to rescue exceptions from the calling method. In fact, I believe this desire comes from a lack of understanding of how blocks/procs work. -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com --Apple-Mail-9-30243354--