From: Robert Klemme Date: 2004-02-20T20:20:17+09:00 Subject: Re: proposal: let kind_of take more arguments "Simon Strandgaard" schrieb im Newsbeitrag news:pan.2004.02.19.14.25.27.611885@adslhome.dk... > It just hit me.. why not let kind_of? take more arguments? > > irb(main):001:0> x = 3 > => 3 > irb(main):002:0> x.kind_of?(String) > => false > irb(main):003:0> x.kind_of?(String, Fixnum) > ArgumentError: wrong number of arguments(2 for 1) > from (irb):3:in `kind_of?' > from (irb):3 > irb(main):004:0> > > > > For instance I have > > 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. > raise "got #{choice.class}" > end > @choices << choice > end > > > It could turn into > > def push(choice) > unless choice.kind_of?(Zero, One) > raise "got #{choice.class}" ArgumentError is a good choice here. :-) > 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 or def push(choice) raise ArgumentError, "got #{choice.class}" unless [Zero, One].map{|cl|choice.kind_of? cl}.any? @choices << choice end or class Object def kind_of_any?( *classes ) classes.each{|cl| return true if kind_of? cl} false end end Then: def push(choice) raise ArgumentError, "got #{choice.class}" unless choice.kind_of_any?(Zero, One) @choices << choice end Regards robert