Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set global default encoding for ruby 1.9

I want to tell ruby that everything is utf8, except when stated otherwise, so I dont have to place these # encoding: utf-8 comments everywhere.

like image 337
grosser Avatar asked May 06 '11 08:05

grosser


2 Answers

If you're using environment variables, the general way is to use LC_ALL / LANG

Neither is set : fallback to US-ASCII

$ LC_ALL= LANG= ruby -e 'p Encoding.default_external'
#<Encoding:US-ASCII>

Either is set : that value is used

$ LC_ALL=en_US.UTF-8 LANG= ruby -e 'p Encoding.default_external'
#<Encoding:UTF-8>

$ LC_ALL= LANG=en_US.UTF-8 ruby -e 'p Encoding.default_external'
#<Encoding:UTF-8>

Both are set : LC_ALL takes precedence

$ LC_ALL=C LANG=en_US.UTF-8 ruby -e 'p Encoding.default_external'
#<Encoding:US-ASCII>

$ LC_ALL=en_US.UTF-8 LANG=C ruby -e 'p Encoding.default_external'
#<Encoding:UTF-8>
like image 58
Arne Brasseur Avatar answered Nov 18 '22 07:11

Arne Brasseur


You can either:

  1. set your RUBYOPT environment variable to "-E utf-8"
  2. or use https://github.com/m-ryan/magic_encoding
like image 16
tommasop Avatar answered Nov 18 '22 06:11

tommasop