Saturday, November 12, 2011

Exercise 15: Reading Files: Learn Ruby The Hard Way: Practicum

So this time we are getting into something that has some meat to it. All of that ARGV and STDIN.gets had a specific purpose. Yes, being able to enter things from the command line are helpful. Interactively typing stuff in for a script is also kind of helpful. By far, though, the most beneficial thing that this offers is the ability to interact with external files; to open them, to read them, to change them, and to use them.

Since we are actually opening and editing files, we have the ability to do some, well, direct and deliberate things to the system, some of which could be dangerous if we are not careful (losing work in simplest case, erasing systems in the worst case). Thus, we need to be careful and pay attention to what we are doing.

We'll be working with two different files. One is our ruby script (ex15.rb) and the other is named ex15_sample.txt. This second file is just a plain text file that we will read into our ruby script.

Here are the contents of ex15_sample.txt file:

This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

Yep, that's it.  A little anti-climactic, maybe, but it'll do :).

So our goal is to open the file in our script and print it out. We don't want to "hard code" the name of the file, though. We want to read it from the command line. That way we can read in other files later. ARGV and STDIN.gets let us dynamically add the file we want to use.

OK, there's a bunch of new things going on in here.

Line 4 is the first use of the File object where we use a new command allows the user to, well, open a file.

Line 7 does something interesting and something we haven't done before. We are making an object of txt and calling the option but using the txt label instead.

What You Should See

$ ruby ex15.rb ex15_sample.txt
Here's your file 'ex15_sample.txt':
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

Type the filename again:
> ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.


Extra Credit:

This is a big jump so be sure you do this extra credit as best you can before moving on.

Above each line write out in English what that line does.

If you are not sure ask someone for help or search online. Many times searching for "ruby THING" will find answers for what that THING does in Ruby. Try searching for "ruby".

[here's a cool example:]

I used the name "commands" here, but they are also called "functions" and "methods". Search around online to see what other people do to define these. Do not worry if they confuse you. It's normal for a programmer to confuse you with their vast extensive knowledge.

[ These come in different flavors depending on who is writing. I've seen functions, procedured, calls, objects, methods, etc. but I get the idea. The point is that we can create certain items and by adding a dot to them we can string commands and possible parameters based on what the parent command allows. The fact that they can also be named as variables or objects is both clarifying and confusing, but stepping back, it makes sense if you are doing something that involves many files or items. Naming them uniquely helps clarify which item is doing what.]

Get rid of the part from line 9-15 where you use STDIN.gets and try the script then.

Use only STDIN.gets and try the script that way. Think of why one way of getting the filename would be better than another.

[While being able to enter the value in the script would be a positive, you would have to be there to enter the name. by adding it on the command line, you can remove the human from the immediate process. this makes it possible to automate the procedure and put it into a script if desired, and it could be run 10,000 times without a person having to enter the file name. those same 10,000 script runs would have to have a human enter the filename each time]

Run ri File and scroll down until you see the read() command (method/function). See all the other ones you can use? Try some of the other commands.

[Well now, this is odd. Maybe it's a limitation to the PC version, but "ri File" only shows me the following:

C:\Users\Michael\LRtHW\Ex15>ri File
←[0m←[1;32mFile < Object←[m

(from gem activesupport-3.0.3)
←[1;32mClass methods:←[m


Also found in:
  gem bundler-1.0.7
  gem thor-0.14.6

and that's it. On my Mac, I got "Nothing known about File".

Startup IRB again and use from the prompt. Notice how you can open files and run read on them right there?

Have your script also do a close() on the txt and txt_again variables. It's important to close files when you are done with them.


So we have made a big jump here. It may not seem like much, but we have now opened up our scripts to the ability of interacting with files, both to read from them and, later, to write to them (come on, you saw that coming, I'm sure ;) ). Having this ability means we can do things with our scripts that can be "hands off" from a human perspective. It also means that we can "pipeline" commands to our scripts via UNIX commands if we want to (granted, right now we're limited to filenames, but the whole point of having ARGV in the first place is to feed variables to the script from the command line, or other scripts). This is helping move us along to scripts that can prove to be very useful.

No comments: