Question : Conducting Regular Expressions on a text file (Ruby)

I'm trying to find and replacace the string "placeholder" in a text file. Any idea how I could get the following code to work?

    file = File.open('/folder/template.txt', 'r+')
    file.gsub!(/placeholder/, "word")

Thanks,

Peter

Answer : Conducting Regular Expressions on a text file (Ruby)

hi,
I could not resist

rehash = {
      "placeholder" => "word",
      "placeholder2" => "other",
}

File.open('test.txt') do |file|
      File.open("sink.rtf", "w") do |sink|
            while line = file.gets
                  sink.write line.gsub(/\[#([^#]*)#\]/){
                        rehash[$1]
                        }
            end
       end
end

cheers

Geert
Random Solutions  
 
programming4us programming4us