From: Dave Vandervort Date: 2001-08-31T12:01:15+09:00 Subject: [ruby-talk:20595] Re: ordered hash > > I just stumbled upon this implementation as I looked through httpmail-ruby: > > class OrderedHash < Hash > alias_method :store, :[]= > alias_method :each_pair, :each > > def initialize > @keys = [] > end > > def []=(key, val) > @keys << key > super > end > > def delete(key) > @keys.delete(key) > super > end > > def each > @keys.each { |k| yield k, self[k] } > end > > def each_key > @keys.each { |k| yield k } > end > > def each_value > @keys.each { |k| yield self[k] } > end > end > > -- > Ryan Tarpine, rtarpine@hotmail.com > "Technology is a queer thing. It brings you great gifts > in one hand, and it stabs you in the back with the other." > -- C.P. Snow Very nice. I remember thinking earlier on this thread that automatically sorted keys would be useful, too. That gives us this: class SortedHash < OrderedHash def []=(key, val) super @keys.sort! end end Thanks. irv