Feature #8639

Add Queue#each

Added by avdi (Avdi Grimm) about 7 years ago. Updated over 3 years ago.

Target version:


I was fiddling around with Queue the other day and realized it doesn't have an #each method. So I made one:

As for why, it makes for a convenient way to build consumer processes:

  inq =
  outq =
  doubler = do
    inq.each do |n|
      outq << n + n

My PR also returns an Enumerator when no block is given, and handles the non_block argument.

I'm sure there's some good reason that this method wasn't there already, so feel free to explain.

Also, this is my first Ruby feature ticket so please let me know if I'm missing any points of protocol.



361.patch (3.34 KB) 361.patch zzak (Zachary Scott), 08/15/2013 06:00 AM

Related issues

Related to Ruby master - Bug #10485: NoMethodError "undefined method `initialize_copy'" when trying to execute Queue#dupClosedActions

Updated by rkh (Konstantin Haase) about 7 years ago

+1 for the feature, should have gone into CommonRuby, I think, maybe.

Updated by avdi (Avdi Grimm) about 7 years ago

On Mon, Jul 15, 2013 at 11:54 AM, rkh (Konstantin Haase) wrote:

+1 for the feature, should have gone into CommonRuby, I think.

Is that something I can change?

Updated by avdi (Avdi Grimm) about 7 years ago

P.S. Should I reply on the ticket or do these conversations eventually get
posted back to it? I'm new to all of this!

Updated by regularfry (Alex Young) about 7 years ago

On Tue, 2013-07-16 at 00:44 +0900, avdi (Avdi Grimm) wrote:

Issue #8639 has been reported by avdi (Avdi Grimm).

I thought this sounded familiar. It has come up before:


Updated by avdi (Avdi Grimm) about 7 years ago

On Mon, Jul 15, 2013 at 4:24 PM, Alex Young wrote:

I thought this sounded familiar. It has come up before:

Hey, thanks for finding that! I figured it had probably come up before.
Some notes responding to that conversation:

  • First, as Alex pointed out then, IO#each is often destructive in the sense that it's either moving the read pointer forward or (in the case of a pipe or socket) losing info once it advances. So there IS a precedent.
  • I feel like the semantics I've implemented in the PR are moderately sane, but feel free to point out anything I've missed.
  • "Loop forever over the things coming out of this queue until something breaks the loop" seems like a common enough use case to warrant some sugar.
  • Note this is NOT "loop until the queue is exhausted", unless you turn non_block on.
  • I am explicitly not discussing the inclusion of Enumerable in this ticket. That's a much bigger can of worms and I haven't even begun to think through all the implications.
  • Note, though, that with this PR if you DO want a full Enumerable over a queue all you have to do now is say q.each, omitting the block. So we're not making Queue enumerable, but we're making it easy to get at a queue Enumerator if you really want one. Without this PR the shortest code I've found to do that is:
   q =
   eq = do |y|
     loop do
       y << q.shift

...and this code doesn't cover all the cases that I've covered in the PR.

Updated by avdi (Avdi Grimm) about 7 years ago

BTW, I feel like I've left out the biggest justification for this, which
for me is POLS. What prompted me to submit the PR was that I actually
started writing an example of using Queue#each---it just made sense for it
to exist, so much so I thought I remembered using it before---and then
realized it wasn't there. To me it felt very natural and intuitive to
expect Queue#each to exist. YMMV, obviously, but #each is such a widespread
convention in Ruby that it seems natural to look for it on a Queue class.

OK, I'll shut up now :-)

Updated by ko1 (Koichi Sasada) about 7 years ago

# Please assume
# @q_buf is current buffer (array) of queue in following example.
# @q_lock is lock of this Queue.

People think Queue#each should be:

(1) Infinite loop

  # iterate forever
  def each
      yield @q_buf.shift

(2) Finite loop

(2-1) Mutable behavior

  # iterate untile elements are exists
  def each
      e = nil
        return if @q_buf.empty?
        e = @q_buf.shift
      yield e

(2-2) Immutable behaviour

  def each(&b)
  # only for chekcking current elements
  # not for inter-thraed communication

# Please add another possible version.

Your proposal is (1). (1) is shorter version of

  while e = q.pop
    break if e == :end

which I wrote frequently.

    break if e == :end

I'm weak negative because
(a) seems not so convinient
(b) #each method is for Enumerable
If we find #each method, then we think it is Enumerable.
(c) we have only few cases for infinite iteration #each methods.
(d) I can think 3 versions described above with in seconds.
(e) I want to use Queue with push' andpop' operations.
Operating with `push' and #each is not consistent.
(Of course, this is my thought. not practical reason)

BTW, I think your `non-blocking' parameter should be a switch of (1) and (2-1).

Updated by ko1 (Koichi Sasada) about 7 years ago

  • Assignee set to ko1 (Koichi Sasada)

Updated by zzak (Zachary Scott) almost 7 years ago

Attaching the patch from Avdi's pull request on github, thank you!

Updated by avdi (Avdi Grimm) almost 7 years ago

Thanks! I'm off to a conf this week, it probably would have been a awhile
before I got around to it.

Avdi Grimm

I only check email twice a day. to reach me sooner, go to

Updated by ko1 (Koichi Sasada) almost 7 years ago

  • Category set to lib
  • Status changed from Open to Feedback
  • Target version set to 2.6

Updated by nobu (Nobuyoshi Nakada) almost 6 years ago

  • Description updated (diff)

Updated by nobu (Nobuyoshi Nakada) almost 6 years ago

  • Related to Bug #10485: NoMethodError "undefined method `initialize_copy'" when trying to execute Queue#dup added

Updated by ko1 (Koichi Sasada) over 3 years ago

  • Status changed from Feedback to Closed

No discussion.

Also available in: Atom PDF