From: Simon Strandgaard Date: 2004-02-20T20:20:37+09:00 Subject: Re: proposal: let kind_of take more arguments On Thu, 19 Feb 2004 16:14:13 +0100, Robert Klemme wrote: > "Simon Strandgaard" schrieb im Newsbeitrag >> On Thu, 19 Feb 2004 15:43:05 +0100, Robert Klemme wrote: >> > >> > 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. > > What do you mean by "late"? Do you mean that the line with "raise" stands > below the actual operation. Didn't think of that, but yes, it might be > confusing. OTOH, if you can read "case" then it's not so difficult, is > it? It isn't clear at first sight when the method bailouts, and if the switch statement spans over many lines, then its not visible at all that its capable of raising exceptions. Kind_of? similar to bouncer pattern, see http://www.rubygarden.org/ruby?BouncerPattern >> >> Confusion :-) > > http://lyricsheaven.topcities.com/survey_d_k_bestanden/ELO.htm#confusion :-) >> > def Object#kind_of_any?( *classes ) >> >> Yes thought of this.. which maked me propose this idea. > > I'm wondering though whether it's worth the effort. When using duck > typing, you don't need kind_of? anyway. > > Btw, I had another idea: > > PUSH_ALLOWED = {Zero, One} > > def push(choice) > raise ArgumentError, "got #{choice.class}" unless > PUSH_ALLOWED[choice.class] > @choices << choice > end Good idea, but gives a complex impression at first sight. > Of course there is a subtle difference since this does not take sub > classes into consideration. But other than that, it should be faster - > especially if you have more classes that shoule be checked. Im in the make it work phase. There is 3 phases: 1) make it work 2) make it right 3) make it fast ;-) -- Simon Strandgaard