MΓ³dulo 04 Β· Idioms, Errors & Gems β LecciΓ³n 4 de 6 Β· ~11 min
Files & JSON
Writing and reading a file
File.write(path, contents) writes a string to disk in one call β no explicit open/close ceremony for the simple case. File.read(path) reads the whole file back as one string:
File.write("notes.txt", "hello")
puts File.read("notes.txt") # helloBoth are class methods on File β no block, no with/defer needed for a one-shot write-then-read. (Ruby also has a block form, File.open(path) { |f| ... }, for streaming line by line, but File.write/File.read cover almost everything you'll need for now.)