From: Massimiliano Mirra Date: 2002-08-04T09:19:40+09:00 Subject: Re: Assoc Class (Hash Pairs) On Sun, Aug 04, 2002 at 08:47:35AM +0900, Tom Sawyer wrote: > i recall having a need for it some time ago, but i don't recall off hand > what that was. but the use i recently ran across was for building > complex hierachies (for gui models) where sometimes you have a "child" > object and some times you don't: > > complex_hierarchy = [ parent => child, > childless, > another_parent => [ subchildless, > subparent => subchild ] > ] > > > without this, you either have to use hashes with extraneous nils, or > fall back to pure associative arrays. This way, the parent won't know it has a child, and the child won't know it has a parent. Is this intended? If so, try out this: class Assoc def Assoc.[](*args) new(*args) end def initialize(from, to nil) @from, @to = from, to end attr_reader :from, :to end class Object def link(to) Assoc[self, to] end end complex_hierarchy = [ Assoc[parent, child], childless, another_parent.link([ subchildless, Assoc[subparent, subchild] ]) Not as nice as a simple `=>', but seems to do what you ask. (I wonder, however, if `=>' could be made into a method of Object.) Massimiliano