instance method
String#scrub
(?::string? replacement) -> ::String
(?nil) { (::String bytes) -> ::string } -> ::String
scrub(replacement_string = default_replacement_string) → new_string
scrub{|sequence| ... } → new_string
string.c
View on GitHub ↗
static VALUE
str_scrub(int argc, VALUE *argv, VALUE str)
{
VALUE repl = argc ? (rb_check_arity(argc, 0, 1), argv[0]) : Qnil;
VALUE new = rb_str_scrub(str, repl);
return NIL_P(new) ? str_duplicate(rb_cString, str): new;
}
Returns a copy of self with each invalid byte sequence replaced by the given replacement_string.
With no block given, replaces each invalid sequence with the given default_replacement_string (by default, "�" for a Unicode encoding, '?' otherwise):
"foo\x81\x81bar"scrub # => "foo��bar"
"foo\x81\x81bar".force_encoding('US-ASCII').scrub # => "foo??bar"
"foo\x81\x81bar".scrub('xyzzy') # => "fooxyzzyxyzzybar"
With a block given, calls the block with each invalid sequence, and replaces that sequence with the return value of the block:
Example
"foo\x81\x81bar".scrub {|sequence| p sequence; 'XYZZY' } # => "fooXYZZYXYZZYbar"
Output :
Example
"\x81" "\x81"
Related: see Converting to New String.
Ruby version changes
-
3.0
-
The following methods now return or yield
Stringinstances instead of subclass instances when called on subclass instances: [Bug #10845]
-