views:

143

answers:

1

I have a problem with iconv tool. I try to call it from rake file in that way:

Dir.glob("*.txt") do |file|
  system("iconv -f UTF-8 -t 'ASCII//TRANSLIT' #{ file } >> ascii_#{ file }")
end

But one file is converted partly (size of partialy converted: 10059092 bytes, before convertion: 10081854). Comparing this two files prove that not all content was writen to ASCII. When I call this command explicit from shell it works perfectly. Other smaller files are converted without problems. Is there any limitations on iconv or Ruby's system()?

A: 

It is always a good idea to check the return value of system to determine whether it was successful.

Dir.glob("*.txt") do |file|
  system("iconv -f UTF-8 -t 'ASCII//TRANSLIT' #{file} >> ascii_#{file}") or
    puts "iconv failed for file #{file}: #{$?}"
end

You could also try using the Iconv standard library, and thus get rid of the system call:

require 'iconv'

source_file = 'utf8.txt'
target_file = 'ascii.txt'

File.open(target_file, 'w') do |file|
  File.open(source_file).each_line do |line|
    file.write Iconv.conv('ASCII//TRANSLIT', 'UTF-8', line)
  end
end

with appropriate error checking added.

Lars Haugseth
Lars I have tried this. system("iconv ...") returns true for all files.
Sebastian
Have you tried redirecting *stderr* to a file to see if iconv outputs anything there?
Lars Haugseth