Repository navigation
Add String#encode :fallback option - #502
Merged
Merged
Conversation
tannevaled
force-pushed
the
feat/encode-fallback
branch
from
August 30, 2026 10:07
38ee9bd to
93105b5
Compare
String#encode's :fallback keyword supplies substitutes for characters that cannot be represented in the target encoding. It accepts a Hash, a Proc, a Method, or any object queried with #[], each passed the offending character as a String; a nil result (a Hash miss included) leaves the character for the normal Encoding::UndefinedConversionError, and a non-String, non-nil result raises TypeError — all matching MRI 4.0.6. The fallback is applied as a pre-pass over the UTF-8 intermediate, rewriting only the genuinely unrepresentable characters (decided per target by charEncodableIn, which covers the core encodings and the x/text-backed legacy ones) so representable characters flow straight to the encoder. undef: :replace pre-empts the fallback entirely, as in MRI. Lifts core/string/encode_spec.rb from 77 to 100 passing examples, byte-for-byte cross-checked against MRI Ruby 4.0.6. ruby/spec ratchet FLOOR 16282 -> 16305 (measured total 16364). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
tannevaled
force-pushed
the
feat/encode-fallback
branch
from
August 30, 2026 10:57
93105b5 to
9140a59
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
String#encode's:fallbackkeyword supplies substitutes for characters that cannot be represented in the target encoding. It accepts aHash, aProc, aMethod, or any object queried with#[], each passed the offending character as aString.Semantics verified byte-for-byte against MRI Ruby 4.0.6:
nilresult (a Hash miss included) leaves the character for the normalEncoding::UndefinedConversionError;String, non-nilresult raisesTypeError;undef: :replacepre-empts the fallback entirely (it is never consulted once undef substitution is on);The fallback is applied as a pre-pass over the UTF-8 intermediate, rewriting only the genuinely unrepresentable characters (decided per target by
charEncodableIn, covering the core encodings and thex/text-backed legacy ones), so representable characters flow straight to the encoder.Conformance: lifts
core/string/encode_spec.rbfrom 77 to 100 passing examples. ruby/spec ratchet FLOOR 16282 → 16305 (measured total 16364).go test ./...green, native +wasip1/wasmbuild, gofmt clean, 100% coverage of the new code.🤖 Generated with Claude Code