Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

String#valid_encoding?

valid_encoding? → true or false

Returns true if self is encoded correctly, false otherwise:

Example
"\xc2\xa1".force_encoding("UTF-8").valid_encoding? # => true
"\xc2".force_encoding("UTF-8").valid_encoding?     # => false
"\x80".force_encoding("UTF-8").valid_encoding?     # => false
See also #upto #upcase! also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0