Zisakis Zavitsanakis
Zisakis Zavitsanakis

Reputation: 359

How to write UTF-8 in file instead of hex

I'm writing a fairly simple Ruby script which follows the following procedure:

  1. Load 500 items from a JSON list
  2. Make a GET request for each one of them and process the data
  3. If the script exits (for a few reasons) remember the last item processed

The first two are done and they're working fairly well.

My problem is on the third part. In order for my script to remember the last processed, I'm writing it in to a text file:

file = File.read('./Formating/Items.json')
last_key = File.open('./last.txt', 'r')
l = last_key.first(1)

data = JSON.parse(file)

puts l
data.each_with_index do |item, index|   

    stuff.do(item)
    last_key.truncate(0)
    last_key.write(index.to_s)

end

The problem appears when I'm using the truncate command with the write command.

Instead of deleting everything from the file and adding the new id as a plain text it adds the new id in HEX.

If I use truncate alone it will work. If I use write alone it will work. When I'm using them together I'm getting the HEX output.

When I'm reading the file it translates the HEX code to a UTF string and I could leave it like that but, instead of deleting the HEX content, it appends the new id at the end, making the file larger.

Is there any way I can fix it?

Upvotes: 0

Views: 62

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121020

I believe the problem is that you are truncating the file, that is currently open.

Quick blind untested fix, that must work:

file = File.read('./Formating/Items.json')
last_key = File.read('./last.txt').to_i rescue 0

data = JSON.parse(file)

puts "Last key is: #{last_key}"
data.each_with_index do |item, index|
    next if index <= last_key # skip already processed  
    # or: data.drop(last_key).each_with_index do |item, index|
    stuff.do(item)
    File.write('./last.txt', index.to_s)
end

Upvotes: 1

Related Questions