Monday, November 14, 2011

Exercise 17: More Files: Learn Ruby The Hard Way: Practicum


So far, we've opened files, we read files, and we've written to files. Now we're going to copy files from one place to another. Are you ready?



Some interesting new stuff this time around. Notice that there is a method called File.exists? I've see this in a lot of code fragments, so I'm going to guess it's somewhat important. It, of course, looks to see if the file is actually there. We are also seeing the ability to get data about a variable and use it inside of the string interpolation/variable substitution areas.


What You Should See


$ ruby ex17.rb test.txt copied.txt
Copying from test.txt to copied.txt
The input file is 81 bytes long
Does the output file exist? False
Ready, hit RETURN to continue, CTRL-C to abort.

Alright, all done.

$ cat copied.txt
To all the people out there.
I say I don't like my hair.
I need to shave it off.
$


Extra Credit

Go read up on Ruby's require statement, and start Ruby to try it out. Try importing some things and see if you can get it right. It's alright if you do not.

[Played with some sample here: http://ruby.about.com/od/rubyfeatures/a/require.htm ]


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 it more friendly to use by removing features.



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

Notice at the end of the WYSS 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.

[As an old UNIX hand, I used cat lots in the 90's. It's been interesting to get back onto a UNIX like system again after a decade of working in Windows shops. It's also interesting to see how many flags cat has, and how many I have never used: http://unixhelp.ed.ac.uk/CGI/man-cgi?cat]

Windows people, find the alternative to cat that Linux/OSX people have. Do not worry about man since there is nothing like that.

[The type command lets us do this. It meets the same need.]

Find out why you had to do output.close() in the code.

[Leaving the file open in too many iterations can lead to memory leaks and too many open files pointers. At least that's how things worked in the old school C days. Ruby may be able to handle this better, but it's still good practice to close the files ].

TESTHEAD's TAKEAWAYS:

So there's a few neat things we are seeing in this example. The first is that the file manipulation commands are fairly straightforward. the second is the fact that we can use attributes of variables to appear in string interpolation commands. we also see that some good practices from earlier languages are still followed in Ruby, if for no other reason than to help us be consistent (I may go back and focus on C again after many years, and it helps to use the same habits in these languages for opening and closing files, even if the language and system doesn't specifically require it).

1 comment:

Lenniel said...

It can be done by one line:

File.open(to_file, 'w').write(File.open(from_file).read())