Prepend a single line to file with Ruby

后端 未结 8 668
再見小時候
再見小時候 2020-12-14 19:35

I\'d like to add a single line to the top a of file with Ruby like this:

# initial file contents
something
else

# file contents after prepending \"hello\" o         


        
8条回答
  •  眼角桃花
    2020-12-14 20:02

    You can try this:

    File.copy_stream(myfile,tempfile)
    f = File.open(myfile,'w')
    f.write("Hello\n#{File.open(tempfile,'r').read}")
    f.close
    File.delete(tempfile)
    

提交回复
热议问题