From: Suraj Kurapati Date: 2011-07-27T03:21:37+09:00 Subject: [ruby-core:38528] [Ruby 1.9 - Feature #4589] add Queue#each() method and include Enumerable Issue #4589 has been updated by Suraj Kurapati. Perhaps this code example can better illustrate my request: class Queue def each(&block) temporary_copy = @internal_queue_lock.synchronize do @internal_item_array.dup end temporary_copy.each(&block) end end Charles Nutter wrote: > At first I thought this would be a good idea. But then I realized > that #each is, in every case I can think of, a non-mutating > operation. Yes, that was my intention: read-only iteration of the queue. > Since I assume you meant for Queue#each to pop all elements off > the queue, this would be the first example I know of a mutating > #each. Sorry if my request was unclear, but that is not what I requested. > Also, what happens when the queue is empty? Does it wait for > another element, or does it end the iteration? It should be non-blocking. Simply iterate over the items that are currently in the queue. If the method was blocking, it would never finish, because we would never reach the "end" of the queue. > The behavior of #each over a queue seems fuzzy to me, and without > a clear specification of what you want I don't see a path forward. I hope my responses above clarify this feature request. Eric Hodel wrote: > I don't think it is appropriate to include Enumerable. Too many > methods from Enumerable seem inappropriate (or behavior would be > application-specific). For example, should #find block, or only > return items that match in the Queue? Since Enumerable relies on #each(), and the Queue#each method I'm requesting is non-blocking, all of the non-destructive Enumerable methods (like #find, #select, #map) should work as we normally expect them to. > Should #reject remove items from the Queue, or only filter? #reject should filter (read-only). #reject! should remove items. > I can see differing needs depending on if the Queue has a single > consumer or multiple consumers. Hmm, shouldn't the thread-safe aspect of Queue take care of that? ---------------------------------------- Feature #4589: add Queue#each() method and include Enumerable http://redmine.ruby-lang.org/issues/4589 Author: Suraj Kurapati Status: Open Priority: Normal Assignee: Category: lib Target version: =begin Please add a thread-safe each() method to the Queue and SizedQueue classes which are provided by the "thread" standard library Also mix-in the Enumerable module into those classes so we can use map/inject/etc. Thanks for your consideration. =end -- http://redmine.ruby-lang.org