[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

appending the contents of multiple text files into 1 file

Paul Danese

6/14/2007 3:20:00 PM

Hi,

is there a simpler/more idiomatic way to append/join the contents of
multiple text files into 1 file?

this works, but i'm trying to see if there are more succinct methods.
thanks!

mynewfile = File.new('C:\mynewfile.txt', 'w')
@myfilenames.each do |mfn|
File.open('#{mfn}.txt') do |file|
file.each_line {|line| mynewfile.puts(line)}
end
end
mynewfile.close


1 Answer

Stefano Crocco

6/14/2007 4:26:00 PM

0

Alle giovedì 14 giugno 2007, Paul Danese ha scritto:
> Hi,
>
> is there a simpler/more idiomatic way to append/join the contents of
> multiple text files into 1 file?
>
> this works, but i'm trying to see if there are more succinct methods.
> thanks!
>
> mynewfile = File.new('C:\mynewfile.txt', 'w')
> @myfilenames.each do |mfn|
> File.open('#{mfn}.txt') do |file|
> file.each_line {|line| mynewfile.puts(line)}
> end
> end
> mynewfile.close

This should work:

File.open('C:\mynewfile.txt','w') do |f|
@myfilenames.each do |mfn|
f.puts(File.read(mfn))
end
end

I hope this helps

Stefano