Opening and closing
Where we used
IO.new to create a new
IO object in the last lesson, we'll use the
File class here. You'll notice it's much more straight-forward!
file.inspect will return a
FakeFS::File -- this isn't a real
File object because otherwise your "friend-list.txt" would conflict with other rubymonk users' "friends-list.txt". Don't worry -- it behaves just like a real
File object. See for yourself!
File#read is shown as an example.)
mode is a string that specifies the way you would like your file to be opened. Here we're using
r+, which opens the file in read-write mode, starting from the beginning.
w opens it in write-only mode, truncating the existing file. You can take look at all the possible modes here.
It's worth noting that there are (many!) multiple ways of opening files in Ruby.
File.open also takes an optional block which will auto-close the file you opened once you are done with it.