Creating an empty file in Ruby: “touch” equivalent?

后端 未结 5 1179
心在旅途
心在旅途 2021-01-30 15:28

What is the best way to create an empty file in Ruby?

Something similar to the Unix command, touch:

touch file.txt
5条回答
  •  抹茶落季
    2021-01-30 16:07

    In Ruby 1.9.3+, you can use File.write (a.k.a IO.write):

    File.write("foo.txt", "")
    

    For earlier version, either require "backports/1.9.3/file/write" or use File.open("foo.txt", "w") {}

提交回复
热议问题