File encoding conversion
When we read a file in Rails application then its charset encoding is assumed to be the same as the application's default. When encoding of the file is different then it might be read incorrectly or application might raise an exception (e.g. invalid UTF-8 character).
We can ensure the proper reading of the file by including the file’s encoding in external_encoding parameter to File#read method:
File.read("/path/to/file.txt", external_encoding: "iso-8859-1")
There is also option to specify internal encoding (in what other encoding the file should be represented - might be helpful when exporting to other systems):
File.read("/path/to/file.txt", internal_encoding: "iso-8859-1")
Written by Tomasz Noworyta
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Related Tags
#rails
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#