Skip to content

Instantly share code, notes, and snippets.

@maerzbow
Created May 29, 2012 23:40
Show Gist options
  • Save maerzbow/2831484 to your computer and use it in GitHub Desktop.
Save maerzbow/2831484 to your computer and use it in GitHub Desktop.
read file with encoding
file = File.open('iso-8859-1.txt', 'r:iso-8859-1')
puts file.external_encoding.name # >> ISO-8859-1
file_content = file.read
puts file_content.encoding.name # >> ISO-8859-1
file = File.open('iso-8859-1.txt', 'r:iso-8859-1:utf-8')
puts file.external_encoding.name # >> ISO-8859-1
file_content = file.read
puts file_content.encoding.name # >> UTF-8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment