Skip to content

Commit 6fbe2dd

Browse files
BurdetteLamarpeterzhu2118
authored andcommitted
[DOC] Tweaks for String#insert
1 parent 823d55a commit 6fbe2dd

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

doc/string/insert.rdoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Inserts the given +other_string+ into +self+; returns +self+.
2+
3+
If the given +index+ is non-negative, inserts +other_string+ at offset +index+:
4+
5+
'foo'.insert(0, 'bar') # => "barfoo"
6+
'foo'.insert(1, 'bar') # => "fbaroo"
7+
'foo'.insert(3, 'bar') # => "foobar"
8+
'тест'.insert(2, 'bar') # => "теbarст" # Characters, not bytes.
9+
'こんにちは'.insert(2, 'bar') # => "こんbarにちは"
10+
11+
If the +index+ is negative, counts backward from the end of +self+
12+
and inserts +other_string+ _after_ the offset:
13+
14+
'foo'.insert(-2, 'bar') # => "fobaro"
15+
16+
Related: see {Modifying}[rdoc-ref:String@Modifying].

string.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6056,19 +6056,9 @@ rb_str_aset_m(int argc, VALUE *argv, VALUE str)
60566056

60576057
/*
60586058
* call-seq:
6059-
* insert(index, other_string) -> self
6059+
* insert(offset, other_string) -> self
60606060
*
6061-
* Inserts the given +other_string+ into +self+; returns +self+.
6062-
*
6063-
* If the Integer +index+ is positive, inserts +other_string+ at offset +index+:
6064-
*
6065-
* 'foo'.insert(1, 'bar') # => "fbaroo"
6066-
*
6067-
* If the Integer +index+ is negative, counts backward from the end of +self+
6068-
* and inserts +other_string+ at offset <tt>index+1</tt>
6069-
* (that is, _after_ <tt>self[index]</tt>):
6070-
*
6071-
* 'foo'.insert(-2, 'bar') # => "fobaro"
6061+
* :include: doc/string/insert.rdoc
60726062
*
60736063
*/
60746064

0 commit comments

Comments
 (0)