Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy question: Read file, reverse it and write to another file in Ruby

Tags:

file-io

ruby

I have:

 o = File.new("ouput.txt", "rw+")
 File.new("my_file.txt").lines.reverse_each { |line|
       ?????  line 
 }
 o.close

I don't know what method to use to write to the file output o

like image 932
OscarRyz Avatar asked Aug 10 '10 00:08

OscarRyz


1 Answers

puts understands arrays, so you can simplify this to:

File.open("f2.txt","w") {|o| o.puts File.readlines("f1.txt").reverse}
like image 75
glenn mcdonald Avatar answered Oct 31 '22 18:10

glenn mcdonald