From: Robert Klemme Date: 2004-03-04T02:04:50+09:00 Subject: Re: Ruby won, of course "Piergiuliano Bossi" schrieb im Newsbeitrag news:c24vu6$jj0$1@lacerta.tiscalinet.it... > 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? - you're omitting the file info as far as I can see - I'd move the v=eval(t) out of the string replacement, no need to have it there. + #inject is even more performant since you omit the intermediate array of results. > Thanks for your feedback. You're welcome! robert > 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 >