7stud -- wrote:
> 
> For extremely large files on the order of 1-2GB, you can try something 
> like this:
> 
> last_line = nil
> temp_file = File.new("temp.txt", "w")
> 
> File.open("data.txt") do |file|
>   file.each do |line|
>     if last_line
>       temp_file.write(last_line)
>     end
> 
>     last_line = line
>   end
> end
> 
> last_line = "hello world\n"
> temp_file.write(last_line)
> temp_file.close()
> 
> #Delete and rename as above


A change of variable names would make it clearer what that code is 
doing:

previous_line = nil
temp_file = File.new("temp.txt", "w")

File.open("data.txt") do |file|
  file.each do |line|
    if previous_line
      temp_file.write(previous_line)
    end

    previous_line = line
  end
end

previous_line = "hello world\n"
temp_file.write(previous_line)
temp_file.close()
-- 
Posted via http://www.ruby-forum.com/.