Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

ruby - Set UTF-8 as default string encoding in Heroku

I need to change the default ruby string encoding to UTF-8 in Heroku. For some reason it is US-ASCII.

$ heroku console
Ruby console for myapp.heroku.com 
>> "a".encoding 
=> #<Encoding:ASCII-8BIT>

However, if I run irb locally I get a different result:

$ irb 
ruby-1.9.2-p136 :001 > "a".encoding 
=> #<Encoding:UTF-8>

Both run on ruby 1.9.2. I've tried setting this as well, but didn't work:

Encoding.default_internal = Encoding.default_external = "UTF-8"

Ideas?

Thanks, Felipe

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As per the Heroku support staff, this is the magic thing:

heroku config:add LANG=en_US.UTF-8

Although heroku console will keep reporting strings encoding as ASCII-8BIT, your actuall app will be running with the correct encoding, based on the LANG config var. You can double check that by doing this:

$ heroku run bash
Running bash attached to terminal... up, run.2
u20415@022e95bf-3ab6-4291-97b1-741f95e7fbda:/app$ irb
irb(main):001:0> "a".encoding
=> #<Encoding:UTF-8>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...