文字列の置換には、sub、sub!、gsub、gsub!メソッドを使う string#sub(置換前パターン,置換後文字列) subは最初のパターンだけを置換。gsubは全てのパターンを置換する。 !をつけると、元のインスタンスの内容を変更する。 サンプル open("hoge.txt"){ |f| open("out.txt","w"){ |o| while line = f.gets line.gsub!("foo","bar") o.puts line end } }open("out2.txt","w"){ |o| open("hoge.txt").each{ |line| o.puts line.gsub("foo","bar") } } ワンライナーでも可能 ruby -e 'puts File.read("in.txt").gsub(/foo/,"bar")' > out.t