From ea28370ef702dc6f78474c9bc1e81307c8c55cb4 Mon Sep 17 00:00:00 2001 From: makicamel Date: Thu, 5 Dec 2024 08:35:07 +0900 Subject: [PATCH] =?UTF-8?q?String#insert=E3=81=AE=E8=AA=AC=E6=98=8E?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第一引数posが負の値の場合String#insertとString#[]=は異なる操作を行う ``` # posが正の場合String#insertとString#[]=は同じ操作を行う str = 'foo'; str.insert(1, 'bar') # => "fbaroo" str = 'foo'; str[1, 0] = 'bar'; str # => "fbaroo" # posが負の場合String#insertとString#[]=は異なる操作を行う str = 'foo'; str.insert(-1, 'bar') # => "foobar" str = 'foo'; str[-1, 0] = 'bar'; str # => "fobaro" ``` --- refm/api/src/_builtin/String | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/refm/api/src/_builtin/String b/refm/api/src/_builtin/String index 0c46028237..decf96af13 100644 --- a/refm/api/src/_builtin/String +++ b/refm/api/src/_builtin/String @@ -1814,6 +1814,14 @@ offset が負の場合、文字列の末尾から数えた位置から探索し #@end --- insert(pos, other) -> self +文字列 other を self に挿入して self を返します。 +pos が正の場合、other を開始インデックスに挿入します。 + +#@samplecode 例 +'foo'.insert(1, 'bar') # => "fbaroo" +#@end + +pos が負の場合、self の末尾から逆方向に数えて pos+1 (self[index] の後) に other を挿入します。 pos 番目の文字の直前に文字列 other を挿入します。 self[pos, 0] = other と同じ操作です。 @@ -1821,13 +1829,9 @@ self[pos, 0] = other と同じ操作です。 @param other 挿入する文字列 #@samplecode 例 -str = "foobaz" -str.insert(3, "bar") -p str # => "foobarbaz" +'foo'.insert(-2, 'bar') # => "fobaro" #@end -@see [[m:String#[]=]] - --- intern -> Symbol --- to_sym -> Symbol