O'Reilly logo

Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World of Computational Thinking with Code, Third Edition, Video Enhanced Edition by Zed A. Shaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercise 17. More Files

Image

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.

ex17.rb

 1    from_file, to_file = ARGV  2  3    puts "Copying from #{from_file} to #{to_file}"  4  5    # we could do these two on one line, how?  6    in_file = open(from_file)  7    indata = in_file.read  8  9    puts "The input file is #{indata.length} bytes long" 10 11    puts "Does the output file exist? #{File.exist?(to_file)}" 12    puts "Ready, hit RETURN to continue, CTRL-C to abort." 13    $stdin ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required