From: Piergiuliano Bossi Date: 2004-03-04T01:09:43+09:00 Subject: Re: Ruby won, of course Robert Klemme wrote: > "Piergiuliano Bossi" schrieb im > Newsbeitrag news:c24rte$da0$1@lacerta.tiscalinet.it... > >>Result: ruby won with the following version >> >>**********************CUT HERE********************** >>def combine(*results) not results.flatten().include? false end >>def check (*all) combine all.collect{|t|c =caller-[caller.last] >> c.collect!{|l|(l.include? "rut.rb")? nil : l.sub(/.*in/, '')} >> puts (eval t)?"pass ...#{c} #{t}":"FAIL ...#{c} #{t}";eval t} end >>**********************CUT HERE********************** > > > IMHO Ruby can do better (also removing some duplicate evaluation): > > def check (*all) c=caller[0...-1].map{|l|l.include?("rut.rb")? nil : > l.sub(/.*in/, '')}.compact > all.collect{|t|res=eval t; puts "#{res ?"pass":"FAIL"} ...#{c} > #{t}";res}.all? end > > irb(main):078:0> check( "5+3", "4>3" ) > pass ...g'g' 5+3 > pass ...g'g' 4>3 > => true > irb(main):079:0> check( "5+3", "4>3", "4<3" ) > pass ...g'g' 5+3 > pass ...g'g' 4>3 > FAIL ...g'g' 4<3 > => false > > Or did I miss something? > You didn't miss anything. Infact in my version (rubytalk:93734) I have done a single evaluation just like what you did. I also prefer my version of caller filtering because it looks much more understandable to me. I have included it here once again just for your own convenience. **********************CUT HERE********************** def check(*tests) stack = caller.map {|m| m[/\w*(?=')/]}.compact.reverse.join " " tests.inject(true) {|ret, t| puts "#{(v=eval(t)) ? 'pass' : 'FAIL'} ..... (#{stack}) #{t.strip}"; ret && v} end **********************CUT HERE********************** What do you think about it? Thanks for your feedback. Ciao, Giuliano -- If you want to send me an email in the address you have to write 'p', then a dot, followed by 'bossi' at 'quinary', another dot and 'com' at last