Post

Writing to a file in ruby

1
2
3
4
5
filename = File.join(__dir__, "example.txt")
test_string = "Hello world"
File.open(filename, "w") do |f|
    f.write(test_string)
end

Often we want to append to a file instead (like a logfile):

1
2
3
4
5
filename = File.join(__dir__, "example.txt")
test_string = "Hello world"
File.open(filename, "a") do |f|
    f.puts(test_string) # 'puts' adds a newline, 'write' doesn't 
end
This post is licensed under CC BY 4.0 by the author.