[DOC] Tweaks for String#scrub (#14892)

This commit is contained in:
Burdette Lamar 2025-10-21 15:48:11 -05:00 committed by GitHub
parent 862b42a52c
commit cb52809ca1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 17 deletions

View File

@ -1,25 +1,22 @@
Returns a copy of +self+ with each invalid byte sequence replaced
by the given +replacement_string+.
With no block given and no argument, replaces each invalid sequence
with the default replacement string
(<tt>"<22>"</tt> for a Unicode encoding, <tt>'?'</tt> otherwise):
With no block given, replaces each invalid sequence
with the given +default_replacement_string+
(by default, <tt>"<22>"</tt> for a Unicode encoding, <tt>'?'</tt> otherwise):
s = "foo\x81\x81bar"
s.scrub # => "foo<6F><6F>bar"
"foo\x81\x81bar"scrub # => "foo<6F><6F>bar"
"foo\x81\x81bar".force_encoding('US-ASCII').scrub # => "foo??bar"
"foo\x81\x81bar".scrub('xyzzy') # => "fooxyzzyxyzzybar"
With no block given and argument +replacement_string+ given,
replaces each invalid sequence with that string:
With a block given, calls the block with each invalid sequence,
and replaces that sequence with the return value of the block:
"foo\x81\x81bar".scrub('xyzzy') # => "fooxyzzyxyzzybar"
"foo\x81\x81bar".scrub {|sequence| p sequence; 'XYZZY' } # => "fooXYZZYXYZZYbar"
With a block given, replaces each invalid sequence with the value
of the block:
"foo\x81\x81bar".scrub {|bytes| p bytes; 'XYZZY' }
# => "fooXYZZYXYZZYbar"
Output:
Output :
"\x81"
"\x81"
Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].

View File

@ -11911,8 +11911,8 @@ enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl, int cr)
/*
* call-seq:
* scrub(replacement_string = default_replacement) -> new_string
* scrub{|bytes| ... } -> new_string
* scrub(replacement_string = default_replacement_string) -> new_string
* scrub{|sequence| ... } -> new_string
*
* :include: doc/string/scrub.rdoc
*