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

后端 未结 5 1180
心在旅途
心在旅途 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 15:52

    If you are worried about file handles:

    File.open("foo.txt", "w") {}
    

    From the docs:

    If the optional code block is given, it will be passed the opened file as an argument, and the File object will automatically be closed when the block terminates.

提交回复
热议问题