Default encoding in ruby 1.9

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

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

···

On Friday 19 June 2009 06:50:19 am Daniel Mendler wrote:

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

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

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

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