 
 This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
| Writing to a File | ||
|---|---|---|
| Code | Expected | Actual | 
| open('output', 'w') { |f| f << "This file contains great truths.\n" }
open('output', 'w') do |f| 
  f.puts 'The great truths have been overwritten with an advertisement.'
end
open('output') { |f| f.read } | "The great truths have been overwritten with an advertisement.\n" | "The great truths have been overwritten with an advertisement.\n" | 
| open('output', "a") { |f| f.puts 'Buy Ruby(TM) brand soy sauce!' }
open('output') { |f| puts f.read } | The great truths have been overwritten with an advertisement. Buy Ruby(TM) brand soy sauce! | The great truths have been overwritten with an advertisement. Buy Ruby(TM) brand soy sauce! | 
| open('output', 'w') do |f|
  [1,2,3].each { |i| f << i << ' and a ' }
end
open('output') { |f| f.read } | "1 and a 2 and a 3 and a " | "1 and a 2 and a 3 and a " | 
| open('output', 'w') do |f|
  f << 'This is going into the Ruby buffer.'
  f.flush                             # Now it's going into the OS buffer.
end
IO.sync = false
open('output', 'w') { |f| f << 'This is going straight into the OS buffer.' } | #<File:output (closed)> | |