From: Javier Valencia Date: 2005-03-01T06:01:03+09:00 Subject: Re: catting files Mark Probert wrote: >Hi .. > >There is approximately an order of magnitude difference in the performance of >these two snippets. Is there a faster way of doing the pure Ruby version? > > >### use the system call 'cat' >def sys_cat > of = "plato.txt.cat" > clean(of) > l = `cat Plato/*.txt > #{of}` >end > > >### open each file and copy it >def rby_cat > of = "plato.txt.rby" > clean(of) > off = File.new(of, "w+") > Dir["Plato/*.txt"].each do |f| > text = IO.readlines(f) > off.puts text > end > off.close >end > > user system total real >sys_cat 0.000000 0.015625 0.117188 ( 0.167577) >rby_cat 0.937500 0.085938 1.023438 ( 1.064247) > > >Thanks, > > > Try mine: def rby_cat File.open("file.rby", "w+") do |file| Dir["*.txt"].each do |f| file.write(IO.readlines(f)) end end end