Forum: Ruby bug in facets 1.8.54

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Nasir K. (Guest)
on 2007-06-10 00:02
(Received via mailing list)
There seems to be a bug in facets PQueue v1.8.54, at least the behavior
inconsistent. Here is the output on machine-1 which is running -

ruby 1.8.5 (2006-08-25) [i386-mswin32]
facets (1.8.51, 1.8.8)

irb(main):001:0> require 'facets/more/pqueue'
=> true
irb(main):002:0> pqueue ={|x,y| x<y})
=> #<PQueue:0x2c35c60 @gt=#<Proc:0x02c35d28@(irb):2>, @size=0,
=> nil
This is the expected behavior

While on another machine with -

ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
facets (1.8.54)

irb(main):001:0> require 'facets/more/pqueue'
=> true
irb(main):002:0> @pqueue ={|x,y| x<y})
ing: default `to_a' will be obsolete
=> <PQueue: size=1, top=#<Proc:0x02d9bc58@(irb):2>>
=> #<Proc:0x02d9bc58@(irb):2>

Strangely the lambda supplied as the comparator is inserted in the
Both Ruby and Facets version is different.

- nasir
Nasir K. (Guest)
on 2007-06-10 00:14
(Received via mailing list)
Ah! pqueue.rb was changed to be backward incompatible from facets
1.8.51 -> 1.8.54

# 1.8.51
 def initialize(compareProc=lambda{|x,y| x>y})
    # By default, retrieves maximal elements first.
    @qarray=[nil]; @size=0; @gt=compareProc; make_legal

# 1.8.54
 def initialize(elements=nil, &block) # :yields: a, b
    @qarray = [nil]
    @size = 0
    @gt = block || lambda {|a,b| a > b}
    replace(elements) if elements

Is there a way to load 1.8.51 version of one file (in this case
pqueue.rb) while use rest of the facets gem 1.8.54?
The other option is to monkey patch :-(
Any other option?

- nasir
Olivier R. (Guest)
on 2007-06-11 00:19
(Received via mailing list)
Le samedi 09 juin 2007 22:14, Nasir K. a écrit :
> pqueue.rb) while use rest of the facets gem 1.8.54?
> The other option is to monkey patch :-(
> Any other option?
> - nasir


Yes you are right, pqueue was rewritten and the api changed a lot. In
there was two different classes for the same purpose, PQueue and Heap,
both provided a heap based priority queue. The latter was broken,
although it
had functionalities that pqueue lacked.

So, I merged both classes into one full-featured class, with a different
closer to the Array one and more ruby-like to me. I submitted it, and as
based my work on pqueue, it replaced the previous version of the class,
Heap was removed.

Here is the notes I transmitted to Trans, who maintains Facets :

* Initialization can take both initial values and comparison block, both
optionnaly. Whereas the original PQueue take only a block, and Heap take
the initial values and needs to be subclassed in order to implement the
* Every methods which take an array as a parameter (initialize,
replace) can now take any object that responds to #to_a, and are
optimized in
the case where the given object is a PQueue itself.
* Creating a queue from scratch (with #initialize or with #replace) is
than for the original PQueue, by adapting the #heapify method from Heap.
* Added consistent methods #inspect, #==, #initialize_copy.
* Methods are documented and unit-tested.

I dropped some methods from both original classes :
* PQueue#make_legal, useless since the PQueue is now always legal
* PQueue#replace_top_low, which I can't figure out the purpose :)
* PQueue#each_with_index, already provided by Enumerator
* Heap#sort_internal, was like an inplace-sort, but it destroyed the

Actually, I didn't know it was already commited :)
If you used only push, pop, clear and empty? in your client code, then
only api change is for the instanciation method, which doesn't take a
object as an argument, but a block.

I see no other solution than monkey patching, if you really don't want
modify your client code.

Nasir K. (Guest)
on 2007-06-11 05:09
(Received via mailing list)
Thanks Olivier.
IMHO  def initialize(compareProc=lambda{|x,y| x>y},  elements=nil)
could have preserved backwards compatibility for PQueue users and also
preserved semantics of passing the comparator as an argument rather than
Shall work around for now.

This topic is locked and can not be replied to.