Module: Modsvaskr::Encoding
- Defined in:
- lib/modsvaskr/encoding.rb
Overview
Provide helpers to encode Windows and Linux strings to UTF-8
Class Method Summary collapse
-
.to_utf_8(str) ⇒ Object
Convert a string to UTF-8.
Class Method Details
.to_utf_8(str) ⇒ Object
Convert a string to UTF-8
- Parameters
-
str (String): The string to convert
- Result
-
String: The converted string
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/modsvaskr/encoding.rb', line 12 def self.to_utf_8(str) orig_encoding = str.encoding encoding = nil begin encoding = %w[ UTF-8 Windows-1252 ISO-8859-1 ].find { |search_encoding| str.force_encoding(search_encoding).valid_encoding? } ensure str.force_encoding(orig_encoding) end raise "Unknown encoding for string #{str[0..127].inspect}" if encoding.nil? str.encode('UTF-8', encoding) end |