Intercepting break within a block after yield

A

Alexey Verkhovsky

--=-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: <[email protected]>
Envelope-to: (e-mail address removed)
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 (e-mail address removed); 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
(e-mail address removed); 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
<[email protected]>; 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 <[email protected]>; 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
<[email protected]>; 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 (e-mail address removed); Wed, 15 Sep 2004 22:50:09
+0400
Delivered-To: (e-mail address removed)
Date: Thu, 16 Sep 2004 03:50:06 +0900
Posted: Tue, 14 Sep 2004 21:51:07 +0300
From: Alexey Verkhovsky <[email protected]>
Reply-To: (e-mail address removed)
To: (e-mail address removed) (ruby-talk ML)
Message-Id: <[email protected]>
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 (e-mail address removed);
help=<mailto:[email protected]?body=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: <mailto:[email protected]>
List-Owner: <mailto:[email protected]>
List-Help: <mailto:[email protected]?body=help>
List-Unsubscribe: <mailto:[email protected]?body=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 elementbreak 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--
 
J

Jamis Buck

Alexey said:
Sorry for posting this without a subject earlier.


------------------------------------------------------------------------

Subject:
No Subject
From:
Alexey Verkhovsky <[email protected]>
Date:
Thu, 16 Sep 2004 03:50:06 +0900
To:
(e-mail address removed) (ruby-talk ML)

To:
(e-mail address removed) (ruby-talk ML)


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

Would something like this work?

class Array
def each_with_a_twist
index = 0
return_list = true
self.each_with_index do |element, index|
yield element
end
rescue Exception
return_list = false
raise
ensure
return_list and return self[0..index]
end
end

a = [ 1, 2, 3, 4, 5 ]
result = a.each_with_a_twist { |i| break "blah" if i > 2 }
p result

result = a.each_with_a_twist { |i| raise "blah" if i > 2 }
p result

--
Jamis Buck
(e-mail address removed)
http://www.jamisbuck.org/jamis

"I use octal until I get to 8, and then I switch to decimal."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top