Ruby - How to write a new file with output from script

后端 未结 2 1187
借酒劲吻你
借酒劲吻你 2021-02-04 00:39

I have a simple script that does some search and replace. This is basically it:

File.open(\"us_cities.yml\", \"r+\") do |file|
  while line = file.gets
  \"do f         


        
2条回答
  •  臣服心动
    2021-02-04 00:55

    First you have to create a new file such as newfile.txt

    Then change the script to

    File.open("us_cities.yml", "r+") do |file|
      new_file = File.new("newfile.txt", "r+")
      while line = file.gets
      new_file.puts "do find a replace"
      end
    end
    

    This will make a new file with the output

提交回复
热议问题