Here is my work for Exercise 20.
The part that immediately drew my attention here was the print_a_line function. I could see the line number was given as an argument and simply printed. The second argument was a file object variable. But the gets.chomp was being called on this file object. How was this giving us one line from the file object at a time? I couldn’t wait for the study drills to figure it out so I looked up the gets method on the I/O class object.
When you run the gets method on the I/O object (a file object is an I/O object), the method reads the next line. It identifies the lines by the new line separators. The file object, f, remembers where the “cursor” is after each read (something it references to remembers). So, every time you call read on the file object, it returns the next line.
The chomp method is called on the string returned, and it removes the new line character at the end.
1. Write English comments for each line to understand what that line does.
2. Each time print_a_line is run, you are passing in a variable current_line. Write out what current_line is equal to on each function call, and trace how it becomes line_count in print_a_line.
The value of current_line in each function call is detailed above (first 1, then 2, then 3). It is the variable passed in to the line_count parameter in the print_a_line function. Through string interpolation, the value of line_count is printed.
3. Find each place a function is used, and check its def to make sure that you are giving it the right arguments.
Yep! The arguments each function takes is detailed in the comments above.
4. Research online what the seek function for file does. Try ri File and see if you can figure it out from there.
In Ruby, a file has a file pointer. This file pointer indicates the current location in a file. When we open a file in read mode, the file pointer is at the beginning of the file. When we read the file, the file pointer moves from the beginning to the end of the file. In this exercise, we call the seek method to move it back to the beginning of the file. The seek method takes an integer amount as the first parameter. The second parameter determines what to do with the integer. The default is IO::SEEK_SET, this seeks to the absolute integer position given by the first parameter. (More reading on the seek method.)
In our example, we give it an integer value of 0. Our rewind method moves the file pointer back to the very beginning of the file.
5. Research the shorthand notation += and rewrite the script to use += instead.
The shorthand notation += is the same as saying adding to the variable. For example x = x + 1, is the same as x+= 1.
We can rewrite current_line = current_line + 1 into current_line += 1.
In this exercise, I learned how to print the contents of a file line by line using the gets method on the file object. I learned how to write functions to open and print file contents. I used the seek method to “rewind” the file pointer back to the beginning of a file object. This exercise also reinforces the use of arguments in a function and how outside variables are not the same as variables inside a function.