From: Robert Klemme Date: 2004-02-20T20:21:10+09:00 Subject: Re: proposal: let kind_of take more arguments "Simon Strandgaard" schrieb im Newsbeitrag news:pan.2004.02.19.15.25.31.227855@adslhome.dk... > 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 I see, you want to save the extra method check_arg_xyz(). :-) > > >> > >> 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. Really? PUSH_ALLOWED[choice.class] looks pretty much like a method call to me... And it's blindingly fast even for large amounts of classes. Well, I guess it's a matter of taste then... You could also use a Set for this like in require 'Set' PUSH_ALLOWED = Set.new([Zero, One]) def push(choice) raise ArgumentError, "got #{choice.class}" unless PUSH_ALLOWED.include? choice.class @choices << choice end Did I say there are tons of ways in Ruby? :-)) > > 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 ;-) :-))) Have fun! robert