简单的问题:读取文件,将其反转并写入Ruby中的另一个文件 [英] Easy question: Read file, reverse it and write to another file in Ruby
本文介绍了简单的问题:读取文件,将其反转并写入Ruby中的另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
o = File.new(ouput.txt,rw +)
File.new(my_file.txt)。lines.reverse_each {| line |
?????行
}
o.close
我不知道用什么方法用于写入文件输出 o
解决方案
puts
理解数组,所以可以简化为:
File.open(f2 .txt,w){| o | o.puts File.readlines(f1.txt)。reverse}
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
解决方案
puts
understands arrays, so you can simplify this to:
File.open("f2.txt","w") {|o| o.puts File.readlines("f1.txt").reverse}
这篇关于简单的问题:读取文件,将其反转并写入Ruby中的另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文