I have a string in Ruby (v1.9.2) that is encoded in UTF-8. I check this via the string and per character:
enc = __ENCODING__
=> #<Encoding:UTF-8>
s.encoding
# => #<Encoding:UTF-8>
s.each_char{|c| return false unless c.encoding == enc }
# this passes
I take the string and write it to a file:
File.open("/path/to/file.rb", "w:UTF-8") do |f|
f.write s
end
# => 39939
File.open("/path/to/file.rb", "rb").read.encoding
# => #<Encoding:ASCII-8BIT>
File.open("/path/to/file.rb", "r").read.encoding
# => #<Encoding:UTF-8>
The file is also set by the text editor I’m using (TextWrangler) to use UTF-8, and it has the magic comment set too. How can I ensure any ruby program (not necessarily under my control) reading this file thinks the encoding is UTF-8?
Note: I do have a specific error I’m trying to fix, but this problem comes up enough that I’d like to have a general answer.
Usually Ruby takes the
$LANGenv variable as a starter, if that one is set to utf-8, ruby should read files as utf-8 by default.