title: gets.chomp inside a function in ruby
body: <p>I'm going through "Learn Ruby the Hard Way" and on exercise 20 there is a snippet of code that I don't understand.  I don't understand why gets.chomp is called on f in the function "print_a_line".</p>
  
  <pre><code>input_file = ARGV.first
  
  def print_all(f)
    puts f.read
  end
  
  def rewind(f)
    f.seek(0)
  end
  
  def print_a_line(line_count, f)
    puts "#{line_count}, #{f.gets.chomp}"
  end
  
  current_file = open(input_file)
  
  puts "First let's print the whole file:\n"
  
  print_all(current_file)
  
  puts "Now let's rewind, kind of like a tape."
  
  rewind(current_file)
  
  puts "Let's print three lines:"
  
  current_line = 1
  print_a_line(current_line, current_file)
  
  current_line = current_line + 1
  print_a_line(current_line, current_file)
  
  current_line = current_line + 1
  print_a_line(current_line, current_file)
  </code></pre>
  
  <p>Consequently, I don't understand how the second part of the output is produced.  I understand it is the first 3 lines of the test.txt file that is passed into the code, but I don't understand how f.gets.chomp produces this. </p>
  
  <pre><code>$ ruby ex20.rb test.txt
  First let's print the whole file:
  This is line 1
  This is line 2
  This is line 3
  Now let's rewind, kind of like a tape.
  Let's print three lines:
  1, This is line 1
  2, This is line 2
  3, This is line 3
  </code></pre>