我以前使用gsub進行正則表達式匹配,但是我應該怎樣調用字符串文字?如何在Ruby中搜索並替換字符串(不是正則表達式)?
我想用pair[1]
代替pair[0]
,無論在文件中找到pair[0]
。
text = File.read(fname)
@hash_old_to_new.each do
|pair|
puts "\tReplacing " + pair[0] + " with " + pair[1]
# result = text.gsub(/pair[0]/, pair[1]) <--- this is no good
end
File.open(fname, "w") { |file| file << result }
gsub? –