Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- (unknown)
Instance Method Summary collapse
-
#scrub(*args) ⇒ Object
If the string is invalid byte sequence then replace invalid bytes with given replacement character, else returns self.
-
#scrub!(*args) ⇒ Object
If the string is invalid byte sequence then replace invalid bytes with given replacement character, else returns self.
Instance Method Details
#scrub ⇒ String #scrub(repl) ⇒ String #scrub {|bytes| ... } ⇒ String
If the string is invalid byte sequence then replace invalid bytes with given replacement character, else returns self. If block is given, replace invalid bytes with returned value of the block.
"abc\u3042\x81".scrub #=> "abc\u3042\uFFFD"
"abc\u3042\x81".scrub("*") #=> "abc\u3042*"
"abc\u3042\xE3\x80".scrub{|bytes| '<'+bytes.unpack('H*')[0]+'>' } #=> "abc\u3042<e380>"
334 335 336 337 338 339 |
# File 'ext/string/scrub.c', line 334
VALUE
rb_str_scrub(int argc, VALUE *argv, VALUE str)
{
VALUE new = str_scrub0(argc, argv, str);
return NIL_P(new) ? rb_str_dup(str): new;
}
|
#scrub! ⇒ String #scrub!(repl) ⇒ String #scrub! {|bytes| ... } ⇒ String
If the string is invalid byte sequence then replace invalid bytes with given replacement character, else returns self. If block is given, replace invalid bytes with returned value of the block.
"abc\u3042\x81".scrub! #=> "abc\u3042\uFFFD"
"abc\u3042\x81".scrub!("*") #=> "abc\u3042*"
"abc\u3042\xE3\x80".scrub!{|bytes| '<'+bytes.unpack('H*')[0]+'>' } #=> "abc\u3042<e380>"
355 356 357 358 359 360 361 |
# File 'ext/string/scrub.c', line 355
static VALUE
str_scrub_bang(int argc, VALUE *argv, VALUE str)
{
VALUE new = str_scrub0(argc, argv, str);
if (!NIL_P(new)) rb_str_replace(str, new);
return str;
}
|