From: ara.t.howard@... Date: 2006-06-02T13:39:00+09:00 Subject: Re: An alternative to the class Foo < Struct.new(vars) idiom and SuperStruct On Fri, 2 Jun 2006 transfire@gmail.com wrote: > > Hal Fulton wrote: >> That's another example of why I would like a data structure >> that 1) had a convenient notation for literals and 2) was >> "like" a Hash, but ordered. > > Facet's has Association which gives one something quite like that. > > [ :a >> 1, :b >> 2 ] > > Of course it does take over the #>> operator, so it can't be used > inside any class that defines it. But that's not a big deal. It just > means you can't _extend_ the following classes with code that uses an > Association. > > Bignum > Fixnum > Date > IPAddr > Process::Status > > T. that just gave me an idea: harp:~ > cat a.rb require 'arrayfields' class Map < Array class Pair < Array; end def initialize *pairs fields = [] pairs.each{|k, v| self << v and fields << k} self.fields = fields end def self.[](*a, &b) new(*a, &b) end end class Symbol def >(value) Map::Pair[self, value] end end def Map(*a, &b) Map.new(*a, &b) end def map(*a, &b) Map.new(*a, &b) end m = map :a > 4, :b > 2 p m p m[:a] p m[:b] p m.to_hash p m.keys harp:~ > ruby -r rubygems a.rb [4, 2] 4 2 {:b=>2, :a=>4} [:a, :b] what do you think? -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama