【发布时间】:2011-12-24 07:00:57
【问题描述】:
我想用 Ruby 在文件的顶部添加一行,如下所示:
# initial file contents
something
else
# file contents after prepending "hello" on its own line
hello
something
else
下面的代码只是替换了整个文件的内容:
f = File.new('myfile', 'w')
f.write "test string"
【问题讨论】: