[DOC] Tweaks for Array#insert (#11709)

This commit is contained in:
Burdette Lamar 2024-09-29 20:21:11 -05:00 committed by GitHub
parent 9b4a497456
commit 154ec2d242
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

42
array.c
View File

@ -2522,38 +2522,38 @@ rb_ary_aset(int argc, VALUE *argv, VALUE ary)
/* /*
* call-seq: * call-seq:
* array.insert(index, *objects) -> self * insert(index, *objects) -> self
* *
* Inserts given +objects+ before or after the element at Integer index +offset+; * Inserts the given +objects+ as elements of +self+;
* returns +self+. * returns +self+.
* *
* When +index+ is non-negative, inserts all given +objects+ * When +index+ is non-negative, inserts +objects+
* before the element at offset +index+: * _before_ the element at offset +index+:
* *
* a = [:foo, 'bar', 2] * a = ['a', 'b', 'c'] # => ["a", "b", "c"]
* a.insert(1, :bat, :bam) # => [:foo, :bat, :bam, "bar", 2] * a.insert(1, :x, :y, :z) # => ["a", :x, :y, :z, "b", "c"]
* *
* Extends the array if +index+ is beyond the array (<tt>index >= self.size</tt>): * Extends the array if +index+ is beyond the array (<tt>index >= self.size</tt>):
* *
* a = [:foo, 'bar', 2] * a = ['a', 'b', 'c'] # => ["a", "b", "c"]
* a.insert(5, :bat, :bam) * a.insert(5, :x, :y, :z) # => ["a", "b", "c", nil, nil, :x, :y, :z]
* a # => [:foo, "bar", 2, nil, nil, :bat, :bam]
* *
* Does nothing if no objects given: * When +index+ is negative, inserts +objects+
*
* a = [:foo, 'bar', 2]
* a.insert(1)
* a.insert(50)
* a.insert(-50)
* a # => [:foo, "bar", 2]
*
* When +index+ is negative, inserts all given +objects+
* _after_ the element at offset <tt>index + self.size</tt>: * _after_ the element at offset <tt>index + self.size</tt>:
* *
* a = [:foo, 'bar', 2] * a = ['a', 'b', 'c'] # => ["a", "b", "c"]
* a.insert(-2, :bat, :bam) * a.insert(-2, :x, :y, :z) # => ["a", "b", :x, :y, :z, "c"]
* a # => [:foo, "bar", :bat, :bam, 2]
* *
* With no +objects+ given, does nothing:
*
* a = ['a', 'b', 'c'] # => ["a", "b", "c"]
* a.insert(1) # => ["a", "b", "c"]
* a.insert(50) # => ["a", "b", "c"]
* a.insert(-50) # => ["a", "b", "c"]
*
* Raises IndexError if +objects+ are given and +index+ is negative and out of range.
*
* Related: see {Methods for Assigning}[rdoc-ref:Array@Methods+for+Assigning].
*/ */
static VALUE static VALUE