From: Ara.T.Howard@... Date: 2004-09-04T02:35:16+09:00 Subject: Re: autochomp? On Fri, 3 Sep 2004, Robert Klemme wrote: > > "Kristof Bastiaensen" schrieb im Newsbeitrag > news:pan.2004.09.02.20.13.44.101750@vleeuwen.org... >> Hi, >> >> On Thu, 02 Sep 2004 19:18:10 +0200, Robert Klemme wrote: >> >>> >>> "Martin DeMello" schrieb im Newsbeitrag >>> news:idIZc.301315$J06.4478@pd7tw2no... >>>> [quoted text muted] >>> >>> Not AFAIK. You can do >>> >>> content = IO.readlines(file).each {|l| l.chomp!} >>> >>> or (more efficient) >>> >>> content = File.open(file) {|io| io.inject([]) {|ar,line| line.chomp!; ar >>> << line} } >>> >> >> I think in this case the first is more efficient than the second. >> Since chomp! modifies the object inplace, no object gets created. > > ??? Please read again. The first is less efficient because it iterates > through the lines of the file twice and both use inplace modification > (#chomp!). what's wrong with IO.foreach? lines = [] IO.foreach(file){|line| lines << line.chomp!} or if you have to have one line IO.foreach(file){|line| (lines||=[]) << line.chomp!} short, effecient, already built-in - what's not to like ;-) i like dave's idea of adding traits to ruby though, if we start now maybe one day ruby will be as good as perl6. i hear it's almost done now! -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | A flower falls, even though we love it; | and a weed grows, even though we do not love it. | --Dogen ===============================================================================