Exercise 17: More Files

Now let's do a few more things with files. We'll write a Ruby script to copy one file to another. It'll be very short but will give you ideas about other things you can do with files.

from_file, to_file = ARGV

puts "Copying from #{from_file} to #{to_file}"

# we could do these two on one line, how?
in_file = open(from_file)
indata = in_file.read

puts "The input file is #{indata.length} bytes long"

puts "Does the output file exist? #{File.exist?(to_file)}"
puts "Ready, hit RETURN to continue, CTRL-C to abort."

out_file = open(to_file, 'w')

puts "Alright, all done."


The important thing to learn from this script is the function File.exist?(to_file) on line 8. This can be broken down as, "File! I want you to use your exist? function to tell me if to_file exists on the disk." Yet another way to say this is, "Get the exist? function from File and call it with the variable to_file." You'll learn more about this later, but for now you should study how you can call functions inside File to do things with files.

What You Should See

Just like your other scripts, run this one with two arguments: the file to copy from and the file to copy it to. I'm going to use a simple test file named test.txt:

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

Alright, all done.

It should work with any file. Try a bunch more and see what happens. Just be careful you do not blast an important file.


Did you see the trick I did with echo to make a file, and cat to show the file? You can learn how to do that in Appendix A.

Study Drills

  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.
  2. See how short you can make the script. I could make this one line long.
  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.
  4. Find out why you had to write out_file.close in the code.

Common Student Questions

Why is the 'w' in quotes?
That's a string. You've been using strings for a while now. Make sure you know what a string is.
No way you can make this one line!
That ; depends ; on ; how ; you ; define ; one ; line ; of ; code.
Is it normal to feel like this exercise was really hard?
Yes, it is totally normal. Programming may not "click" for you until maybe even Exercise 36, or it might not until you finish the book and then make something with Ruby. Everyone is different, so just keep going and keep reviewing exercises that you had trouble with until it clicks. Be patient.

Buy DRM-Free

When you buy directly from the author, Zed A. Shaw, you'll get a professional quality PDF and hours of HD Video, all DRM-free and yours to download and use as you see fit.


Buy Directly From The Author

Or, you can read Learn Ruby the Hard Way for free right here, video lectures not included.