Friday 16 November 2012

UTF-8 encoded Rails Console in linux

Although, initially while working on Rails project, we do not pay much attention to the character encoding, soon it bites us bad. When we try to play with a string containing Unicode characters in an ASCII encoded Rails console, it complains about it and we are stuck.

It is quite easy to get a UTF-8 encoded Rails console. All we need to do is properly set the LANG environment variable of the terminal first.

export LANG=en_US.UTF-8

Rails picks up this variable and sets its encoding. So, when we fire up Rails console now and enter the following command, it shows that we have UTF-8 encoded console.

puts __ENCODING__

Now, we can easily play with UTF-8 strings.

No comments: