From: Johan Martinez Date: 2011-05-20T16:12:12+09:00 Subject: replace lines in a file using hash key-value pairs I want to replace matching lines in a file (config.ini) based on a hash keys. The hash is returned by a yaml file here. Following is a code snippet that I have written. For each key-value pair in hash I want to go through all lines of a file and make string substitution for a matching line. However, this code is not working and I noticed that configfile.each method is executed only once. I appreciate any help on what is wrong with this code.

  configfilename = "configuration.ini"
  puts configfilename

  # deployment properties object
  depprops = DeployProperties.new("deploy.yaml")

  # read config file
  basedir = depprops.get_download["basedir"]

  configfile = File.open("#{configfilename}","r+")

  # read filelocations hash - for each key, replace with value
  depprops.get_filelocations.each_pair do
    |key,value|
    puts " #{key} #{value}"
    configfile.each do
      |line|
      puts line
      # puts line if line.match("#{key}")
      # line.sub!("/#{key}/","#{key} = #{value}")
    end
  end

Also, is there any other way to implement search search-replace for all matching lines in a files based on hash/yaml file. Thanks jM. -- Posted via http://www.ruby-forum.com/.