Skip to content

Instantly share code, notes, and snippets.

@tvandervossen
Created May 22, 2017 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvandervossen/29a6ef984753a5250a575864e13e6ceb to your computer and use it in GitHub Desktop.
Save tvandervossen/29a6ef984753a5250a575864e13e6ceb to your computer and use it in GitHub Desktop.
Read and convert any text file to UTF-8 in Ruby
require 'tedrahcu'
data = IO.binread(path)
encoding = Tedrahcu.detect(data)
data.force_encoding(encoding).encode!('UTF-8', invalid: :replace, undef: :replace, replace: '', universal_newline: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment