Xan Xann wrote: / ... >> #!/usr/bin/ruby >> >> def substitute(source,dest,find,replace) >> data = File.read(source) >> result = data.gsub(/#{find}/,replace) >> File.open(dest,"w") { |f| f.write(result) } if data != result >> end >> >> substitute("temp.txt","result.txt","i","x") >> >> temp.txt = "This is a test line." >> >> result.txt = "Thxs xs a test lxne." >> >> Tested, which I should have done in the first place. > > I get this error: > > wiki.rb:7:in `read': can't convert File into String (TypeError) > from wiki.rb:7:in `substitute' > from wiki.rb:12 > > > with code: > > require 'fileutils' > > f1 = File.open("original.txt") > f2 = File.open("canviada.txt") > > def substitute(source,dest,find,replace) > data = File.read(source) > result = data.gsub(/#{find}/,replace) > File.open(dest,"w") { |f| f.write(result) } if data != result > end > > substitute(f1,f2,"ol","ur") > > > What happens? Here is my example call to 'substitute': substitute("temp.txt","result.txt","i","x") You can see that I use strings containing the names of the files. Here is your example call to 'substitute': f1 = File.open("original.txt") f2 = File.open("canviada.txt") substitute(f1,f2,"ol","ur") You are using open file handles instead of strings. The remedy is, instead of using your example of my example, use my example. -- Paul Lutus http://www.arachnoid.com