From: transfire@... Date: 2006-07-07T06:09:08+09:00 Subject: Re: Achieving Equality Chris Hulan wrote: > I suggest modifying your == method to: > def ==( other ) > #p "#{@read.call} == #{other.inspect}" > if other.kind_of?(RWDelegator) > other == @read.call > else > @read.call == other > end > end > > To compare to other objects you can: > 1) write conversion methods (i.e. to_s) and than convert the RWDelegator > to the appropriate type when comparing (i.e. rwd.to_s == "1") > > 2) re-write the conversion methods of the other classes to handle your > RWDelegator class appropriately: > class String > alias old== == > def ==(other) > if other.kind_of(RWDelegator) > other == self > else > old==(other) > end > end > Thanks! Your suggestions gave me this idea: 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.