One of the cool things about visiting the LCtHW site is that you can see the changes Zed is implementing and when stuff gets updated. What stuff would that be? Well, would you be interested in a PDF version of the book that you can have offline? If so, it’s available for $2.99. Link is here.
So I did do the extra credit from the last assignment, but I am having difficulty with the ri command. It’ll be a project for today to figure out why I’m having trouble with it and see if I can fix it on both my Mac and my PC. Fortunately, Zed doesn’t leave us in the dark on this but explains the values we need to know about. Also, I’m being a bit remiss here. I keep saying Zed did all of this, when the Ruby translation of this book owes a lot to the guy that did the conversion, Rob Sobers. Still, Zed did the original book for Python, and since this is based on that, I think it’s fair to say it’s still Zed asking us to focus our attention, but I want to give Rob some props, too.
So the file commands that are the most important for us to remember are as follow:
- close — Closes the file. Like File->Save.. in your editor.
- read — Reads the contents of the file, you can assign the result to a variable.
- readline — Reads just one line of a text file.
- truncate — Empties the file, watch out if you care about the file.
- write(stuff) — Writes stuff to the file.
So let’s take a look at how this works:
This is the longest file we’ve had to type so far, and here’s a piece of interesting news… this didn’t work on my Mac, which is running with Ruby 1.8 for work reasons. It only works on my PC, which is running Ruby 1.9.2. What’s the difference? It appears that the size parameter isn’t available in Ruby 1.8.7, which is what’s running on my Mac.
What You Should See
There are actually two things you will see, first the output of your new script:
$ 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: To all the people out there.
line 2: I say I don’t like my hair.
line 3: I need to shave it off.
I’m going to write these to the file.
And finally, we close it.
Now, open up the file you made (in my case test.txt) in your editor and check it out. Neat right?
If you feel 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.
Write a script similar to the last exercise that uses read and argv to read the file you just created.
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 6.
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.
[By us asking to write a file, it in addition to making sure we are writing to the file also physically creates the file in the same step]
If you open the file with ‘w’ mode, then do you really need the target.truncate()? Go read the docs for Ruby’s File.open function and see if that’s true.
[I commented out the option for target truncate, and the script worked the same. So no, it is not necessary to run the truncate command if we open the file with ‘w’ access. The danger of course is that we will wipe out whatever is in there, which might not be our goal. Here’s where it pays to make sure :). ]
So we are now able to not just read files, but we have methods to write to them, too. we’re still limited in that anything we write will clobber anything we wrote previously. we will need top have some methods to append text if keeping files are important (I can only assume that that is coming, but hey, the fact I’m asking about it means I’m already mentally focused on it, so I should be ready to tackle it when it come our way 🙂 ).