title: How to read by the number at each iteration of the loop in Ruby?
body: <p>How to read by the number at each iteration of the loop? Dynamic work is important, (not once to read the entire line and convert to an array), at each iteration, take one number from the file string and work with it. How to do it right?</p>
  
  <p>input.txt : </p>
  
  <pre><code>5
  1 7 5 2 3 
  </code></pre>
  
  <p>Work with 2nd line of the file.</p>
  
  <pre><code>fin     = File.open("input.txt", "r")
  fout    = File.open("output.txt", "w")
  n       = fin.readline.to_i
  
  heap_min = Heap.new(:min)
  heap_max = Heap.new(:max)
  
  for i in 1..n
  
      a = fin.read.to_i #code here &lt;-- 
  
      heap_max.push(a)
      if heap_max.size &gt; heap_min.size
          tmp = heap_max.top
          heap_max.pop
          heap_min.push(tmp)
      end 
      if heap_min.size &gt; heap_max.size
          tmp = heap_min.top
          heap_min.pop
          heap_max.push(tmp)
      end 
      if heap_max.size == heap_min.size
          heap_max.top &gt; heap_min.top ? median = heap_min.top : median = heap_max.top
      else
          median = heap_max.top
      end
      fout.print(median, " ")
  end
  </code></pre>