2008/9/21 Gregory Brown <gregory.t.brown / gmail.com>:
> The reason we need to do things this way is that (at least in Ruby,
> but in most languages I'd imagine), there isn't really a way to make a
> change to a file while you're reading it.

Sorry, Greg, but that's not correct. See the following example.

Create a file with three lines:

  name = "test.txt"

  File.open(name, "w") do |file|
    3.times do |num|
      file.puts "line #{num}"
    end
  end

Check the contents of the file:

  puts File.read(name)

Output:

  line 0
  line 1
  line 2

Now read a line, write something, and read a line again:

  File.open(name, "r+") do |file|
    puts file.gets
    file.puts "new data"
    puts file.gets
  end

Output:

  line 0
  ne 2

Read the file again:

  puts File.read(name)

Output:

  line 0
  new data
  ne 2

Regards,
Pit