【发布时间】:2012-05-10 06:37:26
【问题描述】:
只是思考一下关闭我在代码中明确打开的文件的必要性。我来自 C 和 C++ 编程背景,并开始在 Ruby 中导航。提前感谢您的反馈。
from_file, to_file = ARGV
script = $0
puts "Copying from #{from_file} to #{to_file}"
File.open(to_file, 'w').write(File.open(from_file).read())
puts "Alright, all done."
【问题讨论】:
-
如果你只是想复制一个文件,你可以使用
FileUtils.cp。 -
@AndrewMarshall:很高兴现在知道。谢谢!
-
不打开 fd 是不好的做法,就像小便后不拉上拉链是不好的做法一样。