Default encoding in ruby 1.9

D

Daniel Mendler

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
 
S

spox

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
 
D

Daniel Mendler

I have a lot of problems with string encodings in ruby 1.9. I always get
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top