From: knu@... Date: 2015-12-14T07:38:53+00:00 Subject: [ruby-core:72105] [Ruby trunk - Feature #11814] String#valid_encoding? without force_encoding Issue #11814 has been updated by Akinori MUSHA. The first requirement for me was not to modify the original string object, so it should read: str.dup.force_encoding('euc-jp').valid_encoding? instead. This would cost one string object allocation just for testing, but the byte array would be shared while keeping the original object intact. ---------------------------------------- Feature #11814: String#valid_encoding? without force_encoding https://bugs.ruby-lang.org/issues/11814#change-55521 * Author: Usaku NAKAMURA * Status: Rejected * Priority: Normal * Assignee: ---------------------------------------- Now we have to set a encoding to a string to validate it, just like: ```ruby str.force_encoding('euc-jp').valid_encoding? # => true or false ``` But to modify the string is not so smart. knu-san requires the way to validate a string without modifiing it [*1]. Then, I propose to add an optional encoding parameter to `String#valid_encoding?`. ```ruby str.valid_encoding?('euc-jp') # => true or false ``` A patch is attached. [*1] https://twitter.com/knu/status/676009662655934465 (in Japanese) ---Files-------------------------------- valid_encoding.patch (4.4 KB) -- https://bugs.ruby-lang.org/