method
sub!
Related methods
- Class methods (3)
-
new
-
try_convert
(>= v1_9_1_378)
-
yaml_new
- Instance methods (148)
-
<<
-
<=>
-
=~
-
==
-
===
(>= v1_9_1_378)
-
[]
-
[]=
-
*
-
%
-
+
-
ascii_only?
(>= v1_9_1_378)
-
block_scanf
-
bytes
(>= v1_8_7_72)
-
bytesize
(>= v1_8_7_72)
-
byteslice
(>= v1_9_3_125)
-
capitalize
-
capitalize!
-
casecmp
-
center
-
chars
(>= v1_8_7_72)
-
chomp
-
chomp!
-
chop
-
chop!
-
chr
(>= v1_9_1_378)
-
clear
(>= v1_9_1_378)
-
codepoints
(>= v1_9_1_378)
-
concat
-
count
-
crypt
-
delete
-
delete!
-
downcase
-
downcase!
-
dump
-
each
-
each_byte
-
each_char
-
each_codepoint
(>= v1_9_1_378)
-
each_line
-
empty?
-
encode
(>= v1_9_1_378)
-
encode!
(>= v1_9_1_378)
-
encoding
(>= v1_9_1_378)
-
end_regexp
-
end_with?
(>= v1_8_7_72)
-
eql?
-
_expand_ch
-
expand_ch_hash
-
ext
(>= v1_9_3_125)
-
force_encoding
(>= v1_9_1_378)
-
getbyte
(>= v1_9_1_378)
-
gsub
-
gsub!
-
hash
-
hex
-
include?
-
index
-
initialize_copy
-
insert
-
inspect
-
intern
-
is_binary_data?
-
is_complex_yaml?
-
iseuc
-
isjis
(>= v1_9_1_378)
-
issjis
-
isutf8
-
jcount
-
jlength
-
jsize
-
kconv
-
length
-
lines
(>= v1_8_7_72)
-
ljust
-
lstrip
-
lstrip!
-
match
-
mbchar?
-
next
-
next!
-
oct
-
ord
(>= v1_9_1_378)
-
original_succ
-
original_succ!
-
parse_csv
(>= v1_9_1_378)
-
partition
(>= v1_8_7_72)
-
pathmap
(>= v1_9_3_125)
-
pathmap_explode
(>= v1_9_3_125)
-
pathmap_partial
(>= v1_9_3_125)
-
pathmap_replace
(>= v1_9_3_125)
-
prepend
(>= v1_9_3_125)
-
quote
-
_regex_quote
-
replace
-
reverse
-
reverse!
-
rindex
-
rjust
-
rpartition
(>= v1_8_7_72)
-
rstrip
-
rstrip!
-
scan
-
scanf
-
setbyte
(>= v1_9_1_378)
-
shellescape
(>= v1_8_7_72)
-
shellsplit
(>= v1_8_7_72)
-
size
-
slice
-
slice!
-
split
-
squeeze
-
squeeze!
-
start_with?
(>= v1_8_7_72)
-
strip
-
strip!
-
sub
-
sub!
-
succ
-
succ!
-
sum
-
swapcase
-
swapcase!
-
to_c
(>= v1_9_1_378)
-
to_d
(>= v1_9_1_378)
-
toeuc
-
to_f
-
to_i
-
tojis
-
tolocale
(>= v1_9_1_378)
-
to_r
(>= v1_9_1_378)
-
to_s
-
tosjis
-
to_str
-
to_sym
-
toutf16
-
toutf32
(>= v1_9_1_378)
-
toutf8
-
to_yaml
-
tr
-
tr!
-
tr_s
-
tr_s!
-
unpack
-
upcase
-
upcase!
-
upto
-
valid_encoding?
(>= v1_9_1_378)
= private
= protected
sub!(...)
public
Performs the substitutions of String#sub in place, returning str, or nil if no substitutions were performed.
Show source
/* * call-seq: * str.sub!(pattern, replacement) => str or nil * str.sub!(pattern) {|match| block } => str or nil * * Performs the substitutions of <code>String#sub</code> in place, * returning <i>str</i>, or <code>nil</code> if no substitutions were * performed. */ static VALUE rb_str_sub_bang(argc, argv, str) int argc; VALUE *argv; VALUE str; { VALUE pat, repl, match; struct re_registers *regs; int iter = 0; int tainted = 0; long plen; if (argc == 1 && rb_block_given_p()) { iter = 1; } else if (argc == 2) { repl = argv[1]; StringValue(repl); if (OBJ_TAINTED(repl)) tainted = 1; } else { rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc); } pat = get_pat(argv[0], 1); if (rb_reg_search(pat, str, 0, 0) >= 0) { match = rb_backref_get(); regs = RMATCH(match)->regs; if (iter) { char *p = RSTRING(str)->ptr; long len = RSTRING(str)->len; rb_match_busy(match); repl = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match))); str_mod_check(str, p, len); str_frozen_check(str); rb_backref_set(match); } else { repl = rb_reg_regsub(repl, str, regs); } rb_str_modify(str); if (OBJ_TAINTED(repl)) tainted = 1; plen = END(0) - BEG(0); if (RSTRING(repl)->len > plen) { RESIZE_CAPA(str, RSTRING(str)->len + RSTRING(repl)->len - plen); } if (RSTRING(repl)->len != plen) { memmove(RSTRING(str)->ptr + BEG(0) + RSTRING(repl)->len, RSTRING(str)->ptr + BEG(0) + plen, RSTRING(str)->len - BEG(0) - plen); } memcpy(RSTRING(str)->ptr + BEG(0), RSTRING(repl)->ptr, RSTRING(repl)->len); RSTRING(str)->len += RSTRING(repl)->len - plen; RSTRING(str)->ptr[RSTRING(str)->len] = '\0'; if (tainted) OBJ_TAINT(str); return str; } return Qnil; }


