I think what you want to do is to figure out whether your string is properly encoded. The ascii_only?
solution isn't much help when dealing with non-Ascii strings.
I would use String#valid_encoding?
to verify whether a string is properly encoded, even if it contains non-ASCII chars.
For example, what if someone else has encoded "Françoise Paré"
the right way, and when I decode it I get the right string instead of "Fran\xE7oise Par\xE9"
(which is what would be decoded if someone encoded it into ISO-8859-1).
[62] pry(main)> "Françoise Paré".encode("utf-8").valid_encoding?
=> true
[63] pry(main)> "Françoise Paré".encode("iso-8859-1")
=> "Fran\xE7oise Par\xE9"
# Note the encoding is still valid, it's just the way IRB displays
# ISO-8859-1
[64] pry(main)> "Françoise Paré".encode("iso-8859-1").valid_encoding?
=> true
# Now let's interpret our 8859 string as UTF-8. In the following
# line, the string bytes don't change, `force_encoding` just makes
# Ruby interpret those same bytes as UTF-8.
[65] pry(main)> "Françoise Paré".encode("iso-8859-1").force_encoding("utf-8")
=> "Fran\xE7oise Par\xE9"
# Is a lone \xE7 valid UTF-8? Nope.
[66] pry(main)> "Françoise Paré".encode("iso-8859-1").force_encoding("utf-8").valid_encoding?
=> false