From: Alexey Verkhovsky Date: 2004-09-16T03:56:00+09:00 Subject: Intercepting break within a block after yield --=-tXxvyBbO8fivX/O1L+k4 Content-Type: text/plain Content-Transfer-Encoding: 7bit Sorry for posting this without a subject earlier. --=-tXxvyBbO8fivX/O1L+k4 Content-Disposition: inline Content-Description: Forwarded message - No Subject Content-Type: message/rfc822 Return-path: Envelope-to: alex@verk.info Delivery-date: Wed, 15 Sep 2004 22:51:00 +0400 Received: from verk by alfa.pingvist.com with local-bsmtp (Exim 4.34) id 1C7err-0003P2-Kw for alex@verk.info; Wed, 15 Sep 2004 22:51:00 +0400 Received: from [210.251.121.211] (helo=lithium.ruby-lang.org) by alfa.pingvist.com with esmtp (Exim 4.34) id 1C7err-0003Ox-4D for alex@verk.info; Wed, 15 Sep 2004 22:50:59 +0400 Received: from localhost (lithium.ruby-lang.org [127.0.0.1]) by lithium.ruby-lang.org (Postfix) with ESMTP id B8B07C7432; Thu, 16 Sep 2004 03:50:51 +0900 (JST) Received: from lithium.ruby-lang.org ([127.0.0.1]) by localhost (lithium.ruby-lang.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 06570-03; Thu, 16 Sep 2004 03:50:51 +0900 (JST) Received: from lithium.ruby-lang.org (lithium.ruby-lang.org [127.0.0.1]) by lithium.ruby-lang.org (Postfix) with ESMTP id 6746CC67E7; Thu, 16 Sep 2004 03:50:11 +0900 (JST) Received: from localhost (lithium.ruby-lang.org [127.0.0.1]) by lithium.ruby-lang.org (Postfix) with ESMTP id 4D62BC6C26 for ; Thu, 16 Sep 2004 03:50:06 +0900 (JST) Received: from lithium.ruby-lang.org ([127.0.0.1]) by localhost (lithium.ruby-lang.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 06548-03 for ; Thu, 16 Sep 2004 03:50:06 +0900 (JST) Received: from alfa.pingvist.com (unknown [69.22.163.91]) by lithium.ruby-lang.org (Postfix) with ESMTP id B7ABEC67E7 for ; Thu, 16 Sep 2004 03:50:05 +0900 (JST) Received: from 217-16-226-239.dyn-pool.spidernet.net ([217.16.226.239] helo=[10.150.84.216]) by alfa.pingvist.com with esmtp (Exim 4.34) id 1C7er2-0003NJ-NT for ruby-talk@ruby-lang.org; Wed, 15 Sep 2004 22:50:09 +0400 Delivered-To: ruby-talk@ruby-lang.org Date: Thu, 16 Sep 2004 03:50:06 +0900 Posted: Tue, 14 Sep 2004 21:51:07 +0300 From: Alexey Verkhovsky Reply-To: ruby-talk@ruby-lang.org To: ruby-talk@ruby-lang.org (ruby-talk ML) Message-Id: <1095187866.988.14.camel@localhost.localdomain> X-ML-Name: ruby-talk X-Mail-Count: 112659 X-MLServer: fml [fml 4.0.3 release (20011202/4.0.3)]; post only (only members can post) X-ML-Info: If you have a question, send e-mail with the body "help" (without quotes) to the address ruby-talk-ctl@ruby-lang.org; help= X-Mailer: Ximian Evolution 1.4.5 (1.4.5-7.1asp) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - alfa.pingvist.com X-AntiAbuse: Original Domain - ruby-lang.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - verk.info X-Source: X-Source-Args: X-Source-Dir: Mime-Version: 1.0 Content-Type: text/plain Precedence: bulk Lines: 50 List-Id: ruby-talk.ruby-lang.org List-Software: fml [fml 4.0.3 release (20011202/4.0.3)] List-Post: List-Owner: List-Help: List-Unsubscribe: X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at ruby-lang.org X-Spam-Checker-Version: SpamAssassin 2.64 (2004-01-11) on alfa.pingvist.com X-Spam-Status: No, hits=-4.9 required=6.0 tests=BAYES_00 autolearn=ham version=2.64 X-Spam-Level: Subject: No Subject Content-Transfer-Encoding: 7bit A question from Ruby-Forum: http://www.ruby-forum.org/bb/viewtopic.php?t=13 > If I do this: > > Code: > > a = [1, 2, 3] > a.each do |x| > puts x > if x == 2 then break; end > end > > It works (prints 1, 2 then stops), but returns nil. Normally a.each > should return a. > > I need an iterator that will return all elements processed until the > break. E.g. > > Code: > > class Array > def each_with_a_twist > self.each_with_index do > |element, index| > yield element > >>> INTERCEPT THE BREAK <<< > break self[0, index] > end > end > end > > Is it possible? I've come up with the following suggestion: class A def a yield ensure puts "after block"; return 1 end end I can see that this code is not good enough, because it doesn't differentiate between the block terminated by break and the block terminated by an error. What would be the Ruby Way here? Alex --=-tXxvyBbO8fivX/O1L+k4--