Module: ForceUtf8::Encode
Instance Method Summary collapse
Instance Method Details
#encode(string) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/force_utf8/encode.rb', line 3 def encode(string) return unless string string = string.dup encode!(string) string end |
#encode!(string) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/force_utf8/encode.rb', line 10 def encode!(string) return unless string string.force_encoding("UTF-8") unless string.valid_encoding? new_chars = string.chars.map { |c| c.valid_encoding? ? c : '?' }.join string.replace(new_chars) end end |