On 8/21/06, Bil Kleb <Bil.Kleb / nasa.gov> wrote: > Martin DeMello wrote: > > On 8/21/06, Bil Kleb <Bil.Kleb / nasa.gov> wrote: > > > > You can do this automatically (if you aren't already) by creating an > > object that holds a hash and an array, defines []=, each and delete to > > do the right thing, and delegates missing methods to the hash. > > Hmmm... good idea. I've largely missed out on the > whole method_missing idiom. Sounds like a good use. > > I'll try to look into it after I return from JPL next > week. However, if you'd like to throw down an example, > I might be able to work it in now... Quick proof of concept: require 'enumerator' class OHash include Enumerable def initialize @a = [] @h = {} end def []=(k,v) @a.delete(k) if @h[k] @h[k] = v @a << k end def delete(k, &blk) @a.delete(k) @h.delete(k) end def each p @a, @h each_key {|k| yield [k, @h[k]]} end def each_key @a.each {|k| yield k} end def method_missing(*args) @h.send(*args) end end def o(*ary) r = OHash.new ary.each_slice(2) {|k,v| r[k] = v } r end # testing a = o("hello", "world", :foo, "bar", 5, 6) a.each {|k,v| p [k,v]} puts a["hello"] a["hello"] = 5 a.each {|k,v| p [k,v]} martin