From: David Alan Black Date: 2002-08-04T23:28:47+09:00 Subject: Re: Assoc Class (Hash Pairs) Hi -- On Sun, 4 Aug 2002, Tom Sawyer wrote: > On Sun, 2002-08-04 at 07:38, David Alan Black wrote: > > It's an interesting thing to think about, in part because it raises > > the question of what "everything" is. For example, an array is an > > ordered list of elements, where each element can be of any type. So > > -- should there be an ArrayElement type? I tend to think of "hash > > pair" as sort of like "array element" -- I don't mean I think of them > > as the same construct, but rather that, in both cases, I take the > > object-hood of the underlying elements to be sufficient (in terms of > > "everything is an object"), together with the object-hood of the > > containers themselves. > > i think i understand what your getting at. > > but on particulars, there is such a thing as ArrayElement. it is called > Object. for an array's elements are objects (100% O), while a hashes are > not. rather they are some sort of syntatical-construct-of-association > between two objects (nil-O) You could argue that an array is basically a hash whose keys are constrained to be Integers. So that would leave you needing a AssocWhereKeyIsAlwaysAnInteger class, I guess :-) Another way to think of it is that hashes are just arrays which have the ability to return odd-numbered elements from a query based on the previous even-numbered element: class MyHash < Array alias :oldget :[] def [](k) ind = (0...size).detect {|i| i % 2 == 0 and self.at(i) == k} ind ||= size self.oldget(ind+1) end end m = MyHash.new.concat %w{ a b c d e f } p m['c'] # => "d" Dinky example, but you see the point: if arrays are OK, and methods are OK, then if hashes can be defined in terms of arrays and their methods [Q: is this CS'ly sound?], then hashes are OK. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav