From: Joel VanderWerf Date: 2009-02-14T01:29:45+09:00 Subject: Re: performance tips Robert Klemme wrote: > 2008/4/10 Joel VanderWerf : >> Vince Forgetta wrote: >>> Hi all, >>> >>> I've been using ruby for about 2 years, but only recently got around >>> trying >>> and learning it well. >>> >>> Is there some central repository I can get performance tips/discussions >>> such >>> as the one posted here: >>> >>> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/158212 >> Instead of: >> >> lines = File.readlines('sometextfile').each {|l| l.chomp!} >> >> do this: >> >> lines = File.open "test" do |f| >> f.map {|line| line.chomp} >> end >> >> That saves you from having the large string returned by #readlines. > > ??? I could have understood if you argued that your solution avoids > traversing lines twice but this statement is plain wrong: > File.readlines never returned a single String. Oops, I think possibly I meant File.read and map: lines = File.read('sometextfile').map {|l| l.chomp!} Thanks to you, Robert, and to Tom Link for pointing that out as well in an email. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407