> In the more general case, this won't work, as most non-numerics > don't define a coerce method. However, you can use this > (if you feel so inclined) to implement part of Perl's automatic > conversion of strings to numbers in expressions. > > class String > def coerce(other) > case other > when Integer > begin > return Integer(self), other > rescue > return Float(self), Float(other) > end > when Float > return Float(self), other > else super > end > end > end > > 1 + "2" > 1 + "2.3" > 1.2 + "2.3" > 1.5 + "2" But... 1 - "4" => 3 1 - "2.4" => 1.4 The return values were the wrong way around, this should work better: class String def coerce(other) case other when Integer begin return other, Integer(self) rescue return Float(other), Float(self) end when Float return other, Float(self) else super end end end But even so... 8 | "16" TypeError: cannot convert String into Integer from (irb):47:in `|' from (irb):47 from ♥:0 I feel it is an anomaly for the bitwise operators to not call coerce. Yes, it makes no sense to coerce to or from floats for bitwise, but there are other cases as the above shows. Stephen +- S.D.Sykes - www.stephensykes.com | "You are a woman, aren't you?" -- The Duke of Edinburgh checks his | facts after receiving a gift from a woman, Kenya 1984.