ITP - files - simple_file.txt 4

In  this example, we open the file for reading and updating with a+
mode, declare a variable file as a placeholder for the file and append
more text to it with the shovel operator. Note that the file is
automatically closed after the operation (the end of the block) and we
can use puts or write in place of <<.

irb :001 > File.open("simple_file.txt",  "a+") do |file|
irb :002 >       file.write "Writing to files in Ruby is simple."
irb :003?>   end
 => 35
irb :004 >
irb :005 >   File.readlines("simple_file.txt").each_with_index do |line,
line_num|
irb :006 >       puts "#{line_num}: #{line}"
irb :007?>   end
0: another example of writing to a file.
1: Here we are with a new line of textWriting to files in Ruby is
simple.
 => ["another example of writing to a file.\n", "Here we are with a new
line of textWriting to files in Ruby is simple."]