From: Ara.T.Howard@... Date: 2004-09-09T07:15:13+09:00 Subject: Re: OK... :) question about hash and array literals On Thu, 9 Sep 2004, Michael H Buselli wrote: > Sounds like a new data type to me. I frequently find myself doing > things like this: > > @some_data_by_index = Hash.new > @some_data_order = Array.new > > and then I manage the two variables together for any insertion I make > into the data: > > @some_data_by_index[value.key] = value > @some_data_order.push value > > So what if there was an ArrayAndHash? > > @some_data = ArrayAndHash.new(:key) > @some_data.push value > > Such a thing would need some semantics of both an Array and a Hash, and > I don't feel like figuring out what they should look like right now. > Maybe next time I need such a thing I'll think about it more. > > Similar things have been discussed on the list before, but I don't know > if anything became of them. this doesn't solve the OP's problem - but it might solve the above nicely: jib:~ > cat a.rb require 'arrayfields' a = [] a.fields = [] %w(zero one two three).each do |field| a[field] = a.size p a p a.fields end jib:~ > ruby a.rb [0] ["zero"] [0, 1] ["zero", "one"] [0, 1, 2] ["zero", "one", "two"] [0, 1, 2, 3] ["zero", "one", "two", "three"] arrayfields has the semantics that, if the fields have ever been set (even to an empty set), asigning to an unknown field appends. so i automatically does @some_data_by_index[value.key] = value @some_data_order.push value kind regards. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | A flower falls, even though we love it; | and a weed grows, even though we do not love it. | --Dogen ===============================================================================