Reading file name in multiple OS without Ruby encoding problem

I want to get all the file names in UTF-8. For example, after I read the file name in Windows, I do

filename = Iconv.iconv("UTF-8", "Windows-1251", filename) 

In Ubuntu, I do not convert the file name and do not get it in UTF-8. Maybe there is some method for determining the encoding of the OS file name?

+2
source share
1 answer

I do this on Ruby 1.9 when I want everything to be in UTF-8:

 if filename.encoding.to_s != 'UTF-8' filename.encode!('UTF-8') end 

If your OS does not give a file name encoded by an encoding system that does not support some special characters found in the file name, it can be encoded in UTF-8 without hickup.

0
source

All Articles