From: Tony Arcieri Date: 2011-01-27T13:52:25+09:00 Subject: Re: Coercion in Ruby --0016e6de016423b553049accb706 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Jan 26, 2011 at 9:20 PM, David Masover wrote: > Or at least: > > if other.kind_of? TimeInterval > ... > elsif other.kind_of? Numeric > ... > > Remember duck typing? Good point, but that said, it can be done even better: First, in TimeInterval, alias_method :to_int, :value Then when implementing +: if other.respond_to? :to_int TimeInterval.new(value + other.to_int) elsif other.respond_to? :coerce a, b = other.coerce(self) a + b else raise TypeError, "#{other.class} can't be coerced into TimeInterval" end -- Tony Arcieri Medioh! Kudelski --0016e6de016423b553049accb706--