@snmaynard Pretty sure this also doesn't do what you want. There's a feature/bug in String#encode, which means that it doesn't do anything if the source and destination are the same.

The correct code is probably something like:

```ruby
if obj.encoding == Encoding::UTF_8
  obj.encode('utf-16', invalid: :replace, undef: :replace).encode('utf-8') unless obj.valid_encoding?
else
  obj.encode('utf-8', invalid: :replace, undef: :replace)
end
```