Just do the check after the coercion, so something like this:

```
pattern = StringValue(pattern) unless pattern.kind_of?(String)
valid_encoding?(pattern)
```

We also here have to check the string, not only the pattern.