danmcb
June 19, 2009, 3:50pm
1
Hi,
I have a lot of problems with string encodings in ruby 1.9. I always get
EncodingErrors because ruby mixes the default encodings. I have set LANG
and LC_ALL to a utf locale.
test.rb
def encoding_sucks_hard(string)
"hello " + string
end
And then
irb(main):001:0> require ‘test’
=> true
irb(main):002:0> ‘test’.encoding
=> #Encoding:UTF-8
irb(main):003:0> encoding_sucks_hard(‘test’).encoding
=> #Encoding:US-ASCII
irb(main):004:0>
How can I set the default encoding?
Daniel
danmcb
June 19, 2009, 6:08pm
2
On Friday 19 June 2009 06:50:19 am Daniel M. wrote:
"hello " + string
=> #Encoding:US-ASCII
irb(main):004:0>
How can I set the default encoding?
Daniel
Perhaps use the magic comment?
$ irb
irb(main):001:0> a = “foobar”
=> “foobar”
irb(main):002:0> a.encoding
=> #Encoding:UTF-8
irb(main):003:0>
$ irb
irb(main):001:0> #encoding: ASCII-8BIT
irb(main):002:0* a = “foobar”
=> “foobar”
irb(main):003:0> a.encoding
=> #Encoding:ASCII-8BIT
irb(main):004:0> quit
danmcb
June 19, 2009, 10:44pm
3
=> #Encoding:UTF-8
irb(main):003:0>
$ irb
irb(main):001:0> #encoding: ASCII-8BIT
irb(main):002:0* a = “foobar”
=> “foobar”
irb(main):003:0> a.encoding
=> #Encoding:ASCII-8BIT
irb(main):004:0> quit
But what is with libraries? I can’t set their magic comments. The string
literals in the library are therefore incompatible to my string
literals.
Daniel