Files and Directories
Read File
Ruby can read text from a file after another part of the program creates it.
file read
`File.read` loads the whole file content as a string.
Read File
read_file.rb
Replay: real traced execution (multi-file project)
require "tmpdir"
line = "alpha"
Dir.mktmpdir do |dir|
path = File.join(dir, "notes.txt")
File.write(path, "#{line}\nsecond\n")
content = File.read(path)
rows = content.split("\n")
puts "file=#{File.basename(path)}"
puts "first=#{rows.first}"
puts "line_count=#{rows.length}"
end
require "tmpdir"
line = "beta"
Dir.mktmpdir do |dir|
path = File.join(dir, "notes.txt")
File.write(path, "#{line}\nsecond\n")
content = File.read(path)
rows = content.split("\n")
puts "file=#{File.basename(path)}"
puts "first=#{rows.first}"
puts "line_count=#{rows.length}"
end
require "tmpdir"
line = "gamma"
Dir.mktmpdir do |dir|
path = File.join(dir, "notes.txt")
File.write(path, "#{line}\nsecond\n")
content = File.read(path)
rows = content.split("\n")
puts "file=#{File.basename(path)}"
puts "first=#{rows.first}"
puts "line_count=#{rows.length}"
end
line ← alpha
1require "tmpdir"23line→ alpha = "alpha" #@line="beta", "gamma"45Dir.mktmpdir do |dir|6 path = File.join(dir, "notes.txt")7 File.write(path, "#{line}\nsecond\n")89 content = File.read(path)10 rows = content.split("\n")1112 puts "file=#{File.basename(path)}"13 puts "first=#{rows.first}"14 puts "line_count=#{rows.length}"15endpath ← /tmp/⟨tmp A⟩/notes.txt, content ← alpha\nsecond\n, rows ← ["alpha", "second"]
5Dir.mktmpdir do |dir/tmp/⟨tmp A⟩|6 path→ /tmp/⟨tmp A⟩/notes.txt = File.join(dir, "notes.txt")/tmp/⟨tmp A⟩/notes.txt7 File.write(path/tmp/⟨tmp A⟩/notes.txt, "#{linealpha}\nsecond\n")89 content→ alpha\nsecond\n = File.read(path)alpha\nsecond\n10 rows→ ["alpha", "second"] = content.split("\n")["alpha", "second"]1112 puts "file=#{File.basename(path)notes.txt}"13 puts "first=#{rows.firstalpha}"14 puts "line_count=#{rows.length2}"15endoutputfile=notes.txt first=alpha line_count=2
line ← beta
1require "tmpdir"23line→ beta = "beta"45Dir.mktmpdir do |dir|6 path = File.join(dir, "notes.txt")7 File.write(path, "#{line}\nsecond\n")89 content = File.read(path)10 rows = content.split("\n")1112 puts "file=#{File.basename(path)}"13 puts "first=#{rows.first}"14 puts "line_count=#{rows.length}"15endpath ← /tmp/⟨tmp A⟩/notes.txt, content ← beta\nsecond\n, rows ← ["beta", "second"]
5Dir.mktmpdir do |dir/tmp/⟨tmp A⟩|6 path→ /tmp/⟨tmp A⟩/notes.txt = File.join(dir, "notes.txt")/tmp/⟨tmp A⟩/notes.txt7 File.write(path/tmp/⟨tmp A⟩/notes.txt, "#{linebeta}\nsecond\n")89 content→ beta\nsecond\n = File.read(path)beta\nsecond\n10 rows→ ["beta", "second"] = content.split("\n")["beta", "second"]1112 puts "file=#{File.basename(path)notes.txt}"13 puts "first=#{rows.firstbeta}"14 puts "line_count=#{rows.length2}"15endoutputfile=notes.txt first=beta line_count=2
line ← gamma
1require "tmpdir"23line→ gamma = "gamma"45Dir.mktmpdir do |dir|6 path = File.join(dir, "notes.txt")7 File.write(path, "#{line}\nsecond\n")89 content = File.read(path)10 rows = content.split("\n")1112 puts "file=#{File.basename(path)}"13 puts "first=#{rows.first}"14 puts "line_count=#{rows.length}"15endpath ← /tmp/⟨tmp A⟩/notes.txt, content ← gamma\nsecond\n, rows ← ["gamma", "second"]
5Dir.mktmpdir do |dir/tmp/⟨tmp A⟩|6 path→ /tmp/⟨tmp A⟩/notes.txt = File.join(dir, "notes.txt")/tmp/⟨tmp A⟩/notes.txt7 File.write(path/tmp/⟨tmp A⟩/notes.txt, "#{linegamma}\nsecond\n")89 content→ gamma\nsecond\n = File.read(path)gamma\nsecond\n10 rows→ ["gamma", "second"] = content.split("\n")["gamma", "second"]1112 puts "file=#{File.basename(path)notes.txt}"13 puts "first=#{rows.firstgamma}"14 puts "line_count=#{rows.length2}"15endoutputfile=notes.txt first=gamma line_count=2