Reputation: 199215
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
Upvotes: 6
Views: 2793
Reputation: 605
For large files, avoid using readlines, as it'll be really slow/inefficient. Consider using a gem like Elif for this sort of thing.
Upvotes: 0
Reputation: 15478
puts
understands arrays, so you can simplify this to:
File.open("f2.txt","w") {|o| o.puts File.readlines("f1.txt").reverse}
Upvotes: 6
Reputation: 199215
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
Upvotes: 0
Reputation: 14016
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.
Upvotes: 0