From: Tom Sawyer Date: 2002-08-04T11:29:53+09:00 Subject: Re: Assoc Class (Hash Pairs) i'll take a look at this ZigZag. thanks. inspired by you i threw this together. had to use ** instead of => (as you can't use that) and alias ** as 'exp' for the three classes it's used by. just a bit of fun: class Assoc include Comparable attr_accessor :index, :value def Assoc.[](*args) new(*args) end def initialize(index, value=nil) @index = index @value = value end def <=>(assoc) return -1 if self.value < assoc.value return 1 if self.value > assoc.value return 0 if self.value == assoc.value end def reverse! temp = @index @index = @value @value = temp end def to_s return "#{index.to_s}#{value.to_s}" # ? end end class Bignum alias exp ** end class Fixnum alias exp ** end class Float alias exp ** end class Object def **(to) Assoc[self, to] end end complex_hierarchy = [ 'parent' ** 'child', 'childless', 'another_parent' ** [ 'subchildless', 'subparent' ** 'subchild' ] ] now dosen't that look like a strange beast. :-) ~transami On Sat, 2002-08-03 at 19:38, Massimiliano Mirra wrote: > On Sun, Aug 04, 2002 at 09:15:12AM +0900, Tom Sawyer wrote: > > z = ordhash[0]['a'] > > > > or > > > > z = ordhash[0].value > > > > although it may not be out of the question to give Array a little more > > smarts to do what you are suggesting. > > That would lead to ambiguity, e.g.: > > z = { 1 => 'bar', 'foo' => String } > > What will z[1] mean? > > Think of Array as a special case of Hash, where keys are integers and > are set implicitely when an object is added. With a ordered hash you > have two keys, not one, and if you want to access values with the #[] > method, it has to know to which key you are referring. > > > c = 'C'=>'The Letter C' > > puts c['The Letter C'] # --> C > > So are the associations you're thinkin of bidirectional? > > > a = 'A' > > b = 'The Letter A' > > > > a => b > > > > puts a.assoc # --> The Letter A > > Wouldn't that be a.value, according to your examples above? > > > thus having a way to association objects to one another, sort of like > > having varaible variable names. > > You are thinking in terms of bidirectional links between objects. > Take a look at the ZigZag concept, you'll find lots of food for > thought. :-) > > Massimiliano > -- ~transami