3

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.

John Bachir
  • 22,495
  • 29
  • 154
  • 227
  • I have the same question as this person: http://stackoverflow.com/questions/5489647/reading-filename-in-multiple-os-without-encoding-problem-with-ruby – John Bachir Sep 01 '11 at 07:00
  • ruby 1.8 is 'roll-your-own-encoding-stuff', things are different (mostly to the better) with 1.9 – reto Sep 01 '11 at 11:39
  • And check your LC_ALL settings, set it to en_US.utf-8 (see locale -a for possible values), this tells the system to send you only data in utf8. – reto Sep 01 '11 at 11:40

2 Answers2

1

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

#encoding: UTF-8
Nick Nizovtsev
  • 233
  • 1
  • 10
1

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.

raggi
  • 1,290
  • 9
  • 12