From: Rob Biedenharn Date: 2009-08-08T04:47:48+09:00 Subject: Re: How do I add ? On Aug 7, 2009, at 2:27 PM, Thomas Chust wrote: > 2009/8/7 Rob Biedenharn : >> [...] >> puts File.readlines('some_file.txt').inject(0) {|total,line| total + >> line.split("\t")[1].to_i } >> >> (but if the file is large, that could be terribly inefficient) >> [...] > > Hello, > > inject is a method of Iterable, so it's unnecessary to read the whole I think you meant Enumerable > file into memory first to use it. I would prefer a solution that is > both elegant and efficient: > > puts file.inject(0) {|acc, line| acc + line.split(/\t/)[1].to_f } > > cu, > Thomas > > -- > When C++ is your hammer, every problem looks like your thumb. Maybe "Iterable" is something from C++? ;-) I guess I could have simply replaced .readlines with .open in my one- liner. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com