Ignore Encoding Issues when reading files in Ruby 1.9
In case you assume a file is encoded in X but are not sure and do not care if some characters cannot be converted and get lost, try
def slurp(file_name, assumed_encoding, target_encoding)
file_mode = "rb:%s" % assumed_encoding
File.read(file_name, :mode => file_mode).encode(
target_encoding,
:undef => :replace,
:invalid => :replace,
:replace => "?"
).force_encoding(target_encoding)
end
slurp(file_name = "presumably_utf8.txt",
assumed_encoding="UTF-8",
target_encoding="UTF-8")
Written by Matthias Lüdtke
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#