Ruby – How to append a text to file succinctly

ruby

Instead of writing

File.open("foo.txt", "w"){|f| f.write("foo")}

We can write it

File.write("foo.txt", "foo")

Is there simpler way to write this one?

File.open("foo.txt", "a"){|f| f.write("foo")}

Best Solution

This has been answered in great depth already:

can you create / write / append a string to a file in a single line in Ruby

File.write('some-file.txt', 'here is some text', File.size('some-file.txt'), mode: 'a')