Files and Directories
Append File
Opening a file in append mode adds text after the existing content.
append mode
Append mode keeps existing file content and writes new content at the end.
Append File
append_file.rb
Replay: real traced execution (multi-file project)
require "tmpdir"
extra_line = "third"
Dir.mktmpdir do |dir|
path = File.join(dir, "log.txt")
File.write(path, "first\nsecond\n")
file = File.open(path, "a")
file.puts extra_line
file.close
lines = File.read(path).split("\n")
puts "file=#{File.basename(path)}"
puts "lines=#{lines.join(",")}"
puts "last=#{lines.last}"
end
require "tmpdir"
extra_line = "bonus"
Dir.mktmpdir do |dir|
path = File.join(dir, "log.txt")
File.write(path, "first\nsecond\n")
file = File.open(path, "a")
file.puts extra_line
file.close
lines = File.read(path).split("\n")
puts "file=#{File.basename(path)}"
puts "lines=#{lines.join(",")}"
puts "last=#{lines.last}"
end
require "tmpdir"
extra_line = "done"
Dir.mktmpdir do |dir|
path = File.join(dir, "log.txt")
File.write(path, "first\nsecond\n")
file = File.open(path, "a")
file.puts extra_line
file.close
lines = File.read(path).split("\n")
puts "file=#{File.basename(path)}"
puts "lines=#{lines.join(",")}"
puts "last=#{lines.last}"
end
extra_line ← third
1require "tmpdir"23extra_line→ third = "third" #@extra_line="bonus", "done"45Dir.mktmpdir do |dir|6 path = File.join(dir, "log.txt")7 File.write(path, "first\nsecond\n")89 file = File.open(path, "a")10 file.puts extra_line11 file.close1213 lines = File.read(path).split("\n")1415 puts "file=#{File.basename(path)}"16 puts "lines=#{lines.join(",")}"17 puts "last=#{lines.last}"18endpath ← /tmp/⟨tmp A⟩/log.txt, file ← #<File:/tmp/⟨tmp A⟩/log.txt>
5Dir.mktmpdir do |dir/tmp/⟨tmp A⟩|6 path→ /tmp/⟨tmp A⟩/log.txt = File.join(dir, "log.txt")/tmp/⟨tmp A⟩/log.txt7 File.write(path/tmp/⟨tmp A⟩/log.txt, "first\nsecond\n")89 file→ #<File:/tmp/⟨tmp A⟩/log.txt> = File.open(path/tmp/⟨tmp A⟩/log.txt, "a")10 file.puts extra_line(empty)11 file.close1213 lines→ ["first", "second", "third", "third", "third"] = File.read(path).split("\n")["first", "second", "third", "third", "third"]1415 puts "file=#{File.basename(path)log.txt}"16 puts "lines=#{lines.join(",")first,second,third,third,third}"17 puts "last=#{lines.lastthird}"18endoutputfile=log.txt lines=first,second,third,third,third last=third
extra_line ← bonus
1require "tmpdir"23extra_line→ bonus = "bonus"45Dir.mktmpdir do |dir|6 path = File.join(dir, "log.txt")7 File.write(path, "first\nsecond\n")89 file = File.open(path, "a")10 file.puts extra_line11 file.close1213 lines = File.read(path).split("\n")1415 puts "file=#{File.basename(path)}"16 puts "lines=#{lines.join(",")}"17 puts "last=#{lines.last}"18endpath ← /tmp/⟨tmp A⟩/log.txt, file ← #<File:/tmp/⟨tmp A⟩/log.txt>
5Dir.mktmpdir do |dir/tmp/⟨tmp A⟩|6 path→ /tmp/⟨tmp A⟩/log.txt = File.join(dir, "log.txt")/tmp/⟨tmp A⟩/log.txt7 File.write(path/tmp/⟨tmp A⟩/log.txt, "first\nsecond\n")89 file→ #<File:/tmp/⟨tmp A⟩/log.txt> = File.open(path/tmp/⟨tmp A⟩/log.txt, "a")10 file.puts extra_line(empty)11 file.close1213 lines→ ["first", "second", "bonus", "bonus", "bonus"] = File.read(path).split("\n")["first", "second", "bonus", "bonus", "bonus"]1415 puts "file=#{File.basename(path)log.txt}"16 puts "lines=#{lines.join(",")first,second,bonus,bonus,bonus}"17 puts "last=#{lines.lastbonus}"18endoutputfile=log.txt lines=first,second,bonus,bonus,bonus last=bonus
extra_line ← done
1require "tmpdir"23extra_line→ done = "done"45Dir.mktmpdir do |dir|6 path = File.join(dir, "log.txt")7 File.write(path, "first\nsecond\n")89 file = File.open(path, "a")10 file.puts extra_line11 file.close1213 lines = File.read(path).split("\n")1415 puts "file=#{File.basename(path)}"16 puts "lines=#{lines.join(",")}"17 puts "last=#{lines.last}"18endpath ← /tmp/⟨tmp A⟩/log.txt, file ← #<File:/tmp/⟨tmp A⟩/log.txt>
5Dir.mktmpdir do |dir/tmp/⟨tmp A⟩|6 path→ /tmp/⟨tmp A⟩/log.txt = File.join(dir, "log.txt")/tmp/⟨tmp A⟩/log.txt7 File.write(path/tmp/⟨tmp A⟩/log.txt, "first\nsecond\n")89 file→ #<File:/tmp/⟨tmp A⟩/log.txt> = File.open(path/tmp/⟨tmp A⟩/log.txt, "a")10 file.puts extra_line(empty)11 file.close1213 lines→ ["first", "second", "done", "done", "done"] = File.read(path).split("\n")["first", "second", "done", "done", "done"]1415 puts "file=#{File.basename(path)log.txt}"16 puts "lines=#{lines.join(",")first,second,done,done,done}"17 puts "last=#{lines.lastdone}"18endoutputfile=log.txt lines=first,second,done,done,done last=done