How can I get utf8 characters from File#path

2019-05-14 08:16发布

问题:

File#path is giving me Latin-1 characters -- is there a way to get it to give me utf8 characters, or should I just convert what it returns? If so, what's the best/easiest way to convert?

elaboration

So, I know I can do this:

Iconv.new('UTF-8','LATIN1').iconv(File.basename(file.path))

But I'm wondering if there is a more elegant way to tell File to give me utf8 to begin with.

This is especially important because for some reason I get back a different charset on different systems. On my OS X dev machine, it looks like I get back utf8. On my linux server, latin-1.

回答1:

Use a magic comment in a first line of your document:

#encoding: UTF-8


回答2:

See $LANG and $LC_CTYPE (environment variables).

These variables also determine the default value for the encoding defaults in 1.9, and so changes you make today will also work if you later port your code to 1.9.

N.B. Windows is a slightly different beast in this regard, so you may need further information to tackle that.