From: surf Date: 2006-08-26T15:10:10+09:00 Subject: Re: A use case for an ordered hash Hal Fulton wrote: > surf wrote: > > > > Can't you create your own using mixins and including enumerable or > > something like that ? > > > > Yes, but I would lose the convenient literal notation. > > Hal I was curious about this, so I had to try it: ================================================ class OrderedHash include Enumerable def initialize(*parm) @arr = parm @hash = {} parm.each do |p| p.each do |k,v| @hash[k] = v end end end def []=(idx, val) el = {idx => val} @arr << el @hash[idx] = val end def [](idx) @hash[idx] end def each @arr.each do |e| e.each do |k,v| yield k,v end end end end # need each paramter in {}, could also work out a way to do # [:frog,"green", :sky, "blue" etc ] thingColor = OrderedHash.new({:frog => "green"}, {:sky => "blue"}, {:elephant => "pink"}, {:scarf => "red"} ) puts "the sky is #{thingColor[:sky]}\n\n" thingColor[:limo] = "black" thingColor[:snowman] = "white" thingColor.each do |k,v| puts "#{k} = #{v}" end