L
Lui Kore
The encoding of __FILE__ is always the same as Encoding.default_external
even if there is a magic column. Sometimes it is necessary to convert
the string into another encoding. Here is some code to demonstrate the
issue:
#coding: utf-8
# put the script in a not-pure-ascii path to see the difference
path = File.expand_path File.dirname __FILE__
puts RUBY_VERSION + ' ' + RUBY_PLATFORM
#=> "1.9.1 i386-mingw32" is my ruby version
puts path.encoding
#=> "GB2312" on my OS
# usually this "string.encode to, from" works,
# but HERE the new string's content bytes seems unchanged
puts \
path.encode 'utf-8', Encoding.default_external
path.force_encoding Encoding.default_external
puts path.encode 'utf-8'
# changed at last
even if there is a magic column. Sometimes it is necessary to convert
the string into another encoding. Here is some code to demonstrate the
issue:
#coding: utf-8
# put the script in a not-pure-ascii path to see the difference
path = File.expand_path File.dirname __FILE__
puts RUBY_VERSION + ' ' + RUBY_PLATFORM
#=> "1.9.1 i386-mingw32" is my ruby version
puts path.encoding
#=> "GB2312" on my OS
# usually this "string.encode to, from" works,
# but HERE the new string's content bytes seems unchanged
puts \
path.encode 'utf-8', Encoding.default_external
path.force_encoding Encoding.default_external
puts path.encode 'utf-8'
# changed at last