1

Here's what I got:

path_js = 'path/to/a/js/file.js'
path_new_js = 'path/where/the/converted/file/should/go.js'
puts('iconv -f utf-16le -t utf-8 ' + path_js + ' > ' + path_new_js)
system('iconv -f utf-16le -t utf-8 ' + path_js + ' > ' + path_new_js)

The output of the puts statement is:

iconv -f utf-16le -t utf-8 path/to/1-1-2_E1_MC105.js > compiled/path/to/1-1-2_E1_MC105.js

If I copy-paste that exact same line in my terminal the conversion takes place successfully but when it runs inside my ruby script, the new file is created with the same encoding as the original file (utf-16 in this case). Any ideas on what's missing/wrong?

Cheers!

Update: I'm on Mac OS X Snow Leopard and I tried the same script using ruby 1.8.7 (system default) and 1.9.2 (Installed using RVM). I also tried the following:

f = File.open(path_js,'rb')
js = f.read
f.close
new_js = Iconv.conv('utf-8', 'utf-16', js)
File.open(path_new_js,'w'){|f| f.write(new_js)}

With the same outcome :S

Rafael Vega
  • 4,575
  • 4
  • 32
  • 50
  • Are you sure you should be saying UTF-16LE? That means the file has no BOM in it. If you screw up, the output file will have different data from the input file. I bet you should be using UTF-16 not UTF-16LE. Almost never specify LE or BE. – tchrist Aug 19 '11 at 21:53
  • If I use UTF-16 instead of UTF-16LE, the result is the same. – Rafael Vega Aug 21 '11 at 12:09
  • You should get two more bytes with UTF-16 than with UTF-16LE: the BOM is in the first, not the second. – tchrist Aug 21 '11 at 13:57

1 Answers1

0

That should be the equivalent of running the command directly, so be sure it's actually running correctly. system will return false if there's an error in execution.

You can also use the iconv library in Ruby to do it directly instead of needing the command line tool. That may offer more control.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Then it's time to give [iconv](http://www.ruby-doc.org/stdlib/libdoc/iconv/rdoc/index.html) a shot. – tadman Aug 22 '11 at 15:28