title: Remove empty positions in array in Ruby
body: <p>I've created the following method:</p>
  
  <pre><code>def list_all_keys
  $hash_table
  keys=[]
  output_file = File.open("output.txt", "w")
  output_file.write("Hash Table: \n")
  for i in 0..$hash_table.length - 1
    current_node = $hash_table[i]
    puts current_node.val
    while current_node.val != -1
      keys.push("key:" + current_node.key.to_s + ", value:" + current_node.val.to_s + ". pos:" + current_node.pos.to_s)
      current_node = current_node.next
    end
    puts "Hash:" + i.to_s + ", Entries:" + keys.to_s
    output_file.write("Hash:" + i.to_s + ", Entries:" + keys.to_s + "\n")
    keys = []
  end
  output_file.close
  end
  end
  </code></pre>
  
  <p>To write a file <code>output.txt</code> which would contain a hash map of all the values but my output includes a lot of empty positions (because of the while loop, I think).</p>
  
  <p>Example of output:</p>
  
  <pre><code>Hash:3964, Entries:[]    /// CAN THESE BE PREVENTED? ///
  Hash:3965, Entries:["key:quicker., value:1. pos:[3573]"]
  Hash:3966, Entries:["key:easily, value:2. pos:[5639, 10510]"]
  Hash:3967, Entries:["key:kept, value:6. pos:[1732, 1785, 3392, 5932, 7544, 9047]"]
  Hash:3968, Entries:[]
  Hash:3969, Entries:[]
  </code></pre>
  
  <p>Is there some way to prevent this?</p>