[#119637] Behavior of raising from rescue blocks when multiple rescue blocks exist — Rodrigo Rosenfeld Rosas via ruby-core <ruby-core@...>
Hello, I couldn't find any documentation about the subject, so I thought
3 messages
2024/10/29
[ruby-core:119571] [Ruby master Feature#20792] String#forcible_encoding?
From:
"Eregon (Benoit Daloze) via ruby-core" <ruby-core@...>
Date:
2024-10-21 10:52:57 UTC
List:
ruby-core #119571
Issue #20792 has been updated by Eregon (Benoit Daloze). Right. But if it is valid in that encoding, wouldn't you always or almost always then want the String (or a copy of it) in that encoding? If you do, `.with_encoding` would be more efficient as it keeps the computed coderange. If you don't then indeed just a predicate would avoid the String instance allocation (Strings are copy-on-write, so it's just one object allocation, string bytes are not copied). Do you have an example where you wouldn't want a String in that encoding? The [linked example](https://github.com/ruby/prism/blob/d6e9b8de36b4d18debfe36e4545116539964ceeb/lib/prism/parse_result.rb#L15-L30) needs a UTF-8 String on line 19. So `with_encoding` seems a perfect fit there and more efficient than the predicate (1 vs 2 coderange scans). The code also has to explicitly workaround `force_encoding` being inplace (a common inconvenience with `force_encoding`) on line 27, which `with_encoding` addresses. ---------------------------------------- Feature #20792: String#forcible_encoding? https://bugs.ruby-lang.org/issues/20792#change-110185 * Author: kddnewton (Kevin Newton) * Status: Open ---------------------------------------- I would like to add a method to String called `forcible_encoding?(encoding)`. This would return true or false depending on whether the receiver can be forced into the given encoding without breaking the string. It would effectively be an alias for: ```ruby def forcible_encoding?(enc) original = encoding result = force_encoding(enc).valid_encoding? force_encoding(original) result end ``` I would like this method because there are extremely rare but possible circumstances where source files are marked as binary but contain UTF-8-encoded characters. In that case I would like to check if it's possible to cleanly force UTF-8 before actually doing it. The code I'm trying to replace is here: https://github.com/ruby/prism/blob/d6e9b8de36b4d18debfe36e4545116539964ceeb/lib/prism/parse_result.rb#L15-L30. The pull request for the code is here: https://github.com/ruby/ruby/pull/11851. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/