From: Simon Strandgaard Date: 2004-02-20T20:20:25+09:00 Subject: Re: proposal: let kind_of take more arguments On Thu, 19 Feb 2004 15:43:05 +0100, Robert Klemme wrote: > "Simon Strandgaard" schrieb im Newsbeitrag >> >> def push(choice) >> if choice.kind_of?(Zero) == false and choice.kind_of?(One) == false > > Never compare boolean values to get boolean values. This can lead to > hideous bugs if the method at hand does not return "true" or "false" - > especially with languages that have more than one value for either. Thanks for the warning. I have actually had a few problems with it earlier, without realizing this. >> def push(choice) >> unless choice.kind_of?(Zero, One) >> raise "got #{choice.class}" > > ArgumentError is a good choice here. :-) I don't have full overview over Ruby's exception hierarchy, but yes I should use this one here (which I do now). ;-) >> end >> @choices << choice >> end > > Why don't you just do > > def push(choice) > case choice > when Zero, One > @choices << choice > else > raise ArgumentError, "got #{choice.class}" > end > end a switch statement.. easier to read. However I like to do bailout as soon as possible, raising ArgumentError this late may make confusion. > or > > def push(choice) > raise ArgumentError, "got #{choice.class}" unless [Zero, > One].map{|cl|choice.kind_of? cl}.any? > @choices << choice > end Confusion :-) > def Object#kind_of_any?( *classes ) Yes thought of this.. which maked me propose this idea. -- Simon Strandgaard