From: Robert Klemme Date: 2008-10-26T00:24:53+09:00 Subject: Re: Join all text files in a folder, with a single line of Ruby code On 25.10.2008 15:47, William James wrote: > luisbebop wrote: > >> I did a single line of code in Ruby, which joins all text files in a >> folder to a bigfile. I got some tests, and it's works! >> Does anyone knows a better way, or other 'Ruby Way' to do that ? >> >> File.open('bigfile','w') { |mergedFile| Dir.glob("*.txt").each { | >> file| File.readlines(file).each { |line| mergedFile << line } } } > > "We don't need no stinkin' loops!" > > ruby -e"puts ARGF.to_a" *.txt >merged There's also ruby -e '$defout.write(ARGF.read)' *.txt >merged ruby -e 'File.open("out","w") {|io| io.write(ARGF.read)}' *.txt > "We still don't need no stinkin' loops!" > > File.open("mrg","w"){|f|f.puts Dir['*.txt'].map{|nm|IO.read nm}} That's vastly inefficient since it reads all the files into memory before writing a single byte. This is not necessary. You can at least improve to File.open("mrg","w"){|f|Dir['*.txt'].each{|nm|f.write(File.read(nm))}} But a proper solution (i.e. one that deals with arbitrary large files) would use a fixed buffer size - but that looks ugly on a single line... Kind regards robert