Day 18: Live Blogging as I continue Learning Ruby the Hard Way

I’m blogging from a Starbucks in Towson, MD and will be updating this post regularly as I go, so it will be growing and changing until I leave around 2PM!

Right now I am working on Exercise 16: Reading and Writing Files

Here is the file I just typed out:

And the result:

PS C:\Users\Nick> cd\
PS C:\> cd ruby
PS C:\ruby> ruby ex16.rb test.txt
We’re going to erase test.txt
If you don’t want that, hit CTRL-C (^C).
If you do want that, hit RETURN.

Opening the file…
Truncating the file. Goodbye!
Now I’m going to ask you for three lines.
line 1: What a day!
line 2: What a lovely day!
line 3: Witness me, brothers!
And finally, we close it.

So, this exercise shows that, using Ruby, you can create a simple txt file, add lines to it, and close it!

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 above each line will help you understand or at least let you know what you need to research more.

I think I understand it, but its good practice…

And it works removing the truncate line:

PS C:\ruby> ruby ex16.rb test.txt
We’re going to erase test.txt
If you don’t want that, hit CTRL-C (^C).
If you do want that, hit RETURN.

Opening the file…
Truncating the file. Goodbye!
Now I’m going to ask you for three lines.
line 1: Oh say can i see
line 2: by the dawns early lights
line 3: what so proudly we hailed
And finally, we close it.

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

And the result:

PS C:\ruby> ruby studydrill16_2.rb test.txt
Here’s your bloody file, test.txt:
Oh say can i see
by the dawns early lights
what so proudly we hailed

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() command instead of six.

Hmmm….

OK, so I am going to try the “”” lines and call each of the lines inside, using the \n for new lines!

AND IT WORKS!

PS C:\ruby> ruby ex16.rb test.txt
We’re going to erase test.txt
If you don’t want that, hit CTRL-C (^C).
If you do want that, hit RETURN.

Opening the file…
Now I’m going to ask you for three lines.
line 1: What a day!
line 2: What a lovely day!
line 3: Witness me brothers!
And finally, we close it.
PS C:\ruby> ruby studydrill16_2.rb test.txt
Here’s your bloody file, test.txt:

What a day!

What a lovely day!

Witness me brothers!

3. and 4. Find out why we had to pass a ‘w’ as an extra parameter to open. Hint: open tries to be safe by making you explicitly say you want to write a file.
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.

So, the “w” is a write flag. I demonstrated above the truncate isn’t needed by ripping it out while its heart still beat.

Exercise 17: More Files

Here is what I just typed up:

OK, so according to Mr. Shaw, the important part to learn from the above is this:

Which asks whether the file exists on the disk.

SO, I ran it! Here goes:

PS C:\ruby> echo “This is a test file.” > test.txt
PS C:\ruby> cat test.txt
This is a test file.
PS C:\ruby> ruby ex17.rb test.txt new_file.txt
Copying from test.txt to new_file.txt
The input file is 46 bytes long
Does the output file exist? false
Ready, hit RETURN to continue, CTRL-C to abort.

Alright, all done.

So, the interesting thing here is that I before had the test.txt with my quotes from Mad Max: Fury Road. (For the love of God, go see that movie!). I ran this, and ended up with a new file, new_file.txt with “This is a test file.” in it. My test.txt file was also rewritten with this.

Study Drills time:

1. This script is really annoying. There’s no need to ask you before doing the copy, and it prints too much out to the screen. Try to make the script more friendly to use by removing features.

Well, lets delete some stuff…

I say we just want it to write and screw the rest.

PS C:\ruby> echo “Is there anyone home?” > test.txt
PS C:\ruby> cat test.txt
Is there anyone home?
PS C:\ruby> ruby ex17.rb test.txt new_file.txt
Copying from test.txt to new_file.txt
Alright, all done.

Perfect!

2. See how short you can make the script. I could make this one line long.

Hmm. Challenge accepted.

And completed:

PS C:\ruby> echo “Boo!” > test.txt
PS C:\ruby> cat test.txt
Boo!
PS C:\ruby> ruby ex17.rb test.txt new_file.txt

Its as lean as I can I make it. It takes the file names, reads then writes and all done.

3. Notice at the end of the What You Should See I used something called cat? It’s an old command that “con*cat*enates” files together, but mostly it’s just an easy way to print a file to the screen. Type man cat to read about it.

PS C:\ruby> man cat

NAME
Get-Content

SYNTAX
Get-Content [-Path] [-ReadCount ] [-TotalCount ] [-Tail ] [-Filter ] [-Include
] [-Exclude ] [-Force] [-Credential ] [-UseTransaction] [-Delimiter ]
[-Wait] [-Raw] [-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode
| UTF8 | UTF7 | UTF32 | Ascii | Default | Oem}] [-Stream ] []

Get-Content -LiteralPath [-ReadCount ] [-TotalCount ] [-Tail ] [-Filter ]
[-Include ] [-Exclude ] [-Force] [-Credential ] [-UseTransaction] [-Delimiter
] [-Wait] [-Raw] [-Encoding {Unknown | String | Unicode | Byte |
BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem}] [-Stream ] []

ALIASES
gc
cat
type

REMARKS
Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
— To download and install Help files for the module that includes this cmdlet, use Update-Help.
— To view the Help topic for this cmdlet online, type: “Get-Help Get-Content -Online” or
go to http://go.microsoft.com/fwlink/?LinkID=113310.

4. Find out why you had to write out_file.close in the code.

From everything I read, it is proper and necessary to always close any file you open.

OK, I have to leave Starbucks to go pick up my wife from work. I will return tonight!