Chris Conley wrote:
> Hello,
> 
> I'm trying to insert new text into the middle of a file with the
> following:
> 
> file = File.open("file.rb", "r+")
> file.each { |line|
>   if line.match(/line text/)
>     file.puts("hello")
>   end
> }
> 
> This works fine except that it overwrites the existing 5 characters to
> replace with "hello". Is there a way to insert a new line with new
> text without overwriting any existing data?
> 
> Thanks!
> Chris

try changing:
file = File.open("file.rb", "r+")

for:
file = File.open("file.rb", "a+")


that should do it!

-- 
Posted via http://www.ruby-forum.com/.