def try_with_encoding(string, encoding)
      result = yield string
      return result if result

      unless encoding == string.encoding
        string = string.encode(encoding)
        yield string
      end
    end
def try_with_encoding(string, encoding)
      result = yield string
      return result if result

      unless encoding == string.encoding
        string = string.encode(encoding)
        yield string
      end
    end
Tries an operation using `string` directly. If the operation fails, the
 string is copied and encoded with `encoding` and the operation is tried
 again.

 @param string [String] The `String` to perform the operation on.
 @param encoding [Encoding] The `Encoding` to use if the initial attempt
   fails.
 @yield [s] the caller will be yielded to once or twice to attempt the
   operation.
 @yieldparam s [String] either `string` or an encoded copy of `string`.
 @yieldreturn [Object] The result of the operation. Must be truthy if
   successful.
 @return [Object] the result of the operation or `nil` if the first attempt
 fails and `string` is already encoded with `encoding`.