Day 3.4 Exercise 16 Reading and Writing

Here’s my work.
Here’s the link to exercise 16.

Ex16 Code
Ex16 Code
Ex16 Output
Ex16 Output
Ex16 Open Methods
Extra information on file opening methods

Study drills

1. If you do not understand this, go back through and use the comment trick to get it squared away in your mind. One simple english comment on each line will help you understand or at least let you know what you need to research more?
Do that if you don’t understand.

2. Write a script similar to the last exercise that uses read and argv to read the file you just created.
Ex16 Study Drill

3. There’s too much repetition in this file. Use Strings, formats, and escapes to print out line1, line2, and line3 with just one target.write() instead of six.
I used concantenation and remove .chomp after gets. See the first picture for more info.

4. Find out why we had to pass a ‘w’ as an extra parameter to open. Hint: open tries to be safe by explicitly say you want to write a file.
By default, we can only read the file. Read 3rd picture for detail explanation and other methods for open.

5. If you open the file with ‘w’ mode, then do you really need the target.truncate()? Read the documentation for Ruby’s open function and see if that’s true.
There’s no need for truncate in ‘w’ mode. Read above for details.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s