From: transfire@... Date: 2006-07-07T16:04:24+09:00 Subject: Re: Achieving Equality Srinivas JONNALAGADDA wrote: > transfire@gmail.com wrote: > > class RWDelegator > > > > def initialize( write, &read ) > > @read = read > > @write = write > > > > read_class = @read.call.object_class > > > > unless read_class.method_defined?(:eq_with_rwdelegator?) > > read_class.class_eval %{ > > def eq_with_rwdelegator?( other ) > > if RWDelegator === other > > other == self > > else > > eq_without_rwdelegator?(other) > > end > > end > > alias_method :eq_without_rwdelegator?, :== > > alias_method :==, :eq_with_rwdelegator? > > } > > end > > end > > > > ... > > > > T. > > > > > > The above approach would not scale seamlessly should you have another > RWDelegator-like class. Any suggestions to fix? > I would be interested in knowing the situation that required something > like this. It is for method Annotations. The problem here is that annotations are inherited. So when readin them you want to see the merged result from the current class (receiver) up through Kernel itself. But when you are writting to them you want to just effect the the current class. Here's an example:: class Y ann :a, :foo => [1] end class X < Y ann :a, :bar => [2] end Y.ann.a #=> # X.ann.a #=> # X.ann.a.foo << 2 X.ann.a #=> # Y.ann.a #=> # In the past, one had to do X.ann.a.foo! << 2 for this to work. Perhaps the overhead of th RWDelegator is too much and I should go back to the original way? Even so, it was quite a challenge to ge the above to work. T.