From: David Masover Date: 2011-01-27T13:20:35+09:00 Subject: Re: Coercion in Ruby On Wednesday, January 26, 2011 02:52:12 pm Tony Arcieri wrote: > Just a small nitpick: > > if other.is_a? TimeInterval > TimeInterval.new(value + other.value) > elsif other.is_a? Numeric > TimeInterval.new(value + other) > else > raise TypeError, "#{other.class} can't be coerced into TimeInterval" > end > > would probably be better written as: > > case other > when TimeInterval > TimeInterval.new(value + other.value) > when Numeric > TimeInterval.new(value + other) > else raise TypeError, "#{other.class} can't be coerced into TimeInterval" > end Or at least: if other.kind_of? TimeInterval ... elsif other.kind_of? Numeric ... Remember duck typing? I don't care if it is_a Numeric, not really. I can't really think of many cases where is_a? makes more sense than kind_of?.