2013-12-29 22:05:25 -05:00
|
|
|
class String
|
|
|
|
# A poor man's scrub, Ruby 2.1 has a much better implementation, but this will do
|
|
|
|
unless method_defined? :scrub
|
2017-07-27 21:20:09 -04:00
|
|
|
def scrub(replace_char = nil)
|
2013-12-29 22:05:25 -05:00
|
|
|
str = dup.force_encoding("utf-8")
|
|
|
|
|
|
|
|
unless str.valid_encoding?
|
|
|
|
# work around bust string with a double conversion
|
2017-07-27 21:20:09 -04:00
|
|
|
str.encode!("utf-16", "utf-8", invalid: :replace)
|
|
|
|
str.encode!("utf-8", "utf-16")
|
2013-12-29 22:05:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|