3
我想将文本预先添加到现有文件中。此代码工作得很好,但最后一行似乎从未执行。我可以删除原始文件,但该文件从来没有在新的临时文件回原来的文件名重命名......试图预先使用红宝石
我敢肯定它的东西很容易,但是,我不知道为什么它不工作。谁能帮忙?
# grab input text
s = "{query}"
# create insert value in template with timestamp
tmp = "#{Time.now.strftime('%Y-%m-%d %I:%M %p')}\n#{s}"
# path to file you wish to append...
# folder path must exist, file will be created if it doesn't
o = File.expand_path("~/Dropbox/Notes/1scratchpad.txt")
n = File.expand_path("~/Dropbox/Notes/1scratchpad.new.txt")
# open file in append mode and add the string
File.open(n, 'w') do |n|
n.puts tmp
n.puts "\n"
File.foreach(o) do |li|
n.puts li
end
end
File.delete(o)
File.rename(n, o)
能不能请你'p File.delete(O)'向我们展示了输出。 – 2013-05-08 05:53:29
这对我来说是写作的。 Ruby 1.9.3-p392,Mac OS X Server 10.5.8 PPC – Substantial 2013-05-08 05:53:42
追加模式为'a'' FYI。 – squiguy 2013-05-08 05:57:33