From: Robert Klemme Date: 2004-12-04T00:32:41+09:00 Subject: Re: ordered hash ? "David A. Black" schrieb im Newsbeitrag news:Pine.LNX.4.44.0412030658240.26976-100000@wobblini... > Hi -- > > On Fri, 3 Dec 2004, Robert Klemme wrote: > > > > I guess I'd prefer to see hash order be a > > > characteristic of every hash (without an exception raised), regardless > > > > Please, not of every Hash - just the OrderedHashes. IMHO space and time > > overheads are significant enough to not include this into every hash. > > Right, just to the extent that a Hash is Ordered in the first place. > (I had the feeling Matz was considering having all of them be ordered, > but that may be wrong and/or a different matter.) Hm, might be. I can't remember such a statement but then again - I don't read every single posting. So this might have slipped through my fingers. Still I don't think it's a good idea to make all hashes insertion ordered. I could imagine a solution that uses a callback instance for greatest flexibility. That way even insertion order could be easily realized: class OrderedHash include Enumerable # callbacks for various events class InstanceCallbacks def hash_instance(o) o.hash end def insert(key,val) end def update(key,val) end def remove(key,val) end def compare(a,b) a<=>b end end class InsertionOrderCB < InstanceCallbacks def initialize() @ins = [] end def insert(key,val) @ins << key end def remove(key,val) @ins.delete key end def compare(a,b) return 0 if a <=> == 0 @ins.each do |x| if a <=> x == 0 return -1 elsif b <=> x == 0 return 1 end end raise "Internal Error" end end def initialize(ih = InsertionOrderCB.new,&b) @ih = ih # ... end def store(key,val) # use @ih instance handler to determine order... end def delete(key) # ... end end Kind regards robert