From: "Hal E. Fulton" Date: 2002-08-04T09:45:20+09:00 Subject: Re: Assoc Class (Hash Pairs) ----- Original Message ----- From: "Tom Sawyer" To: "ruby-talk ML" Sent: Saturday, August 03, 2002 7:15 PM Subject: Re: Assoc Class (Hash Pairs) > On Sat, 2002-08-03 at 16:40, Hal E. Fulton wrote: > > Well, it's ordered, all right; but is it really a hash > > any more? E.g., could you still get this to work? > > > > z = ordhash['a'] # equals 1 > > > > Not without changing the [] method of Array. And if you > > change it in such a way, you're making Array into Hash. > > no, not exactly. it's still array. you'd have to say: > > z = ordhash[0]['a'] > > or > > z = ordhash[0].value Yes, but that's my point: It's not a hash any more. It's like an array of single-element hashes. To find the value associated with key 'a', I'd have to *know* that 'a' was in ordhash[0]. Someone yesterday (?) posted an interesting use of Array#assoc... I think this is probably the "right" way to implement an ordered hash... and I wish I had thought of that two years ago. > a = 'A'=>'The Letter A' > b = 'B'=>'The Letter B' > c = 'C'=>'The Letter C' > > puts a.index # --> A > puts b.value # --> The Letter B > > puts a['A'] # --> The Letter A > puts c['The Letter C'] # --> C Hmm. Maybe. > a = 'A' > b = 'The Letter A' > > a => b > > puts a.assoc # --> The Letter A > > thus having a way to association objects to one another, sort of like > having varaible variable names. Incompatible with the earlier suggestion, of course, since a => b would be a valid Assoc value. > just thinking out loud at this point. Frequently a helpful thing! Hal