From: pjb@... (Pascal J. Bourguignon) Date: 2009-01-09T19:09:45+09:00 Subject: Re: Encoding issue for special characters on Windows Nicolas Gaiffe writes: > Hi, > > I am facing an issue with special characters handling inside a Ruby > script running on Windows and am sure some of you could help me on > this. > > This script copies files such as ".txt" to > ".txt". But once translated, the new filename may > have special characters. '�' for instance. > > Running > puts '�' > in a Ruby script gives > '�' > as an output, whereas the same code in irb gives > '�' > > There must be an encoding issue at some point in my script but I > didn't manage to fix it (tried different values of '#encoding:' > without success). Any clue ? I use emacs. In emacs, you'd just put: #!/usr/bin/ruby # -*- coding:utf-8 -*- puts "�" to have the script encoded in utf-8 and therefore outputing an utf-8 byte stream. Then of course, you have to have an utf-8 terminal: [pjb@simias :0.0 tmp]$ chmod 755 test.rb [pjb@simias :0.0 tmp]$ export LC_CTYPE=en_US.UTF-8 [pjb@simias :0.0 tmp]$ ./test.rb � [pjb@simias :0.0 tmp]$ cat test.rb #!/usr/bin/ruby # -*- coding:utf-8 -*- puts "�" [pjb@simias :0.0 tmp]$ Notice that in irb, with an utf-8 terminal, "�".length == 2 Of course, you can choose to use iso-8859-1 or iso-8859-15, just substitute utf-8. -- __Pascal Bourguignon__