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
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
I knew it was easy, what I don't quite get is why is not documented here?
o = File.new("ouput.txt", "w+")
File.new("my_file.txt").lines.reverse_each { |line|
o.puts line
}
o.close
You're gonna wanna do something more like...
new_text = File.readlines('my_file').reverse.join
File.open('my_file', 'w+') { |file| file.write(new_text) }
Check out this documentation to figure out what w+
means.
puts
understands arrays, so you can simplify this to:
File.open("f2.txt","w") {|o| o.puts File.readlines("f1.txt").reverse}