How can I get utf8 characters from File#path

2019-05-14 08:00发布

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.

2条回答
We Are One
2楼-- · 2019-05-14 08:32

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.

查看更多
等我变得足够好
3楼-- · 2019-05-14 08:53

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

#encoding: UTF-8
查看更多
登录 后发表回答