From: Burdette Lamar Date: Wed, 16 Oct 2024 18:52:34 +0000 (-0500) Subject: [DOC] Tweaks for Array#slice! (#11902) X-Git-Tag: v3_4_0_rc1~1175 X-Git-Url: https://repo.or.cz/ruby.git/commitdiff_plain/4621c9b8154533e96a3f53de823578b1e171aa03 [DOC] Tweaks for Array#slice! (#11902) --- diff --git a/array.c b/array.c index 015395f74c..42601a5f82 100644 --- a/array.c +++ b/array.c @@ -4145,71 +4145,94 @@ ary_slice_bang_by_rb_ary_splice(VALUE ary, long pos, long len) /* * call-seq: - * array.slice!(n) -> object or nil - * array.slice!(start, length) -> new_array or nil - * array.slice!(range) -> new_array or nil + * slice!(index) -> object or nil + * slice!(start, length) -> new_array or nil + * slice!(range) -> new_array or nil * * Removes and returns elements from +self+. * - * When the only argument is an Integer +n+, - * removes and returns the _nth_ element in +self+: + * With numeric argument +index+ given, + * removes and returns the element at offset +index+: * - * a = [:foo, 'bar', 2] - * a.slice!(1) # => "bar" - * a # => [:foo, 2] + * a = ['a', 'b', 'c', 'd'] + * a.slice!(2) # => "c" + * a # => ["a", "b", "d"] + * a.slice!(2.1) # => "d" + * a # => ["a", "b"] * - * If +n+ is negative, counts backwards from the end of +self+: + * If +index+ is negative, counts backwards from the end of +self+: * - * a = [:foo, 'bar', 2] - * a.slice!(-1) # => 2 - * a # => [:foo, "bar"] + * a = ['a', 'b', 'c', 'd'] + * a.slice!(-2) # => "c" + * a # => ["a", "b", "d"] + * + * If +index+ is out of range, returns +nil+. * - * If +n+ is out of range, returns +nil+. + * With numeric arguments +start+ and +length+ given, + * removes +length+ elements from +self+ beginning at zero-based offset +start+; + * returns the removed objects in a new array: * - * When the only arguments are Integers +start+ and +length+, - * removes +length+ elements from +self+ beginning at offset +start+; - * returns the deleted objects in a new +Array+: + * a = ['a', 'b', 'c', 'd'] + * a.slice!(1, 2) # => ["b", "c"] + * a # => ["a", "d"] + * a.slice!(0.1, 1.1) # => ["a"] + * a # => ["d"] * - * a = [:foo, 'bar', 2] - * a.slice!(0, 2) # => [:foo, "bar"] - * a # => [2] + * If +start+ is negative, counts backwards from the end of +self+: + * + * a = ['a', 'b', 'c', 'd'] + * a.slice!(-2, 1) # => ["c"] + * a # => ["a", "b", "d"] + * + * If +start+ is out-of-range, returns +nil+: + * + * a = ['a', 'b', 'c', 'd'] + * a.slice!(5, 1) # => nil + * a.slice!(-5, 1) # => nil * * If start + length exceeds the array size, * removes and returns all elements from offset +start+ to the end: * - * a = [:foo, 'bar', 2] - * a.slice!(1, 50) # => ["bar", 2] - * a # => [:foo] + * a = ['a', 'b', 'c', 'd'] + * a.slice!(2, 50) # => ["c", "d"] + * a # => ["a", "b"] * * If start == a.size and +length+ is non-negative, - * returns a new empty +Array+. + * returns a new empty array. * * If +length+ is negative, returns +nil+. * - * When the only argument is a Range object +range+, - * treats range.min as +start+ above and range.size as +length+ above: + * With Range argument +range+ given, + * treats range.min as +start+ (as above) + * and range.size as +length+ (as above): * - * a = [:foo, 'bar', 2] - * a.slice!(1..2) # => ["bar", 2] - * a # => [:foo] + * a = ['a', 'b', 'c', 'd'] + * a.slice!(1..2) # => ["b", "c"] + * a # => ["a", "d"] * - * If range.start == a.size, returns a new empty +Array+. + * If range.start == a.size, returns a new empty array: * - * If range.start is larger than the array size, returns +nil+. + * a = ['a', 'b', 'c', 'd'] + * a.slice!(4..5) # => [] * - * If range.end is negative, counts backwards from the end of the array: + * If range.start is larger than the array size, returns +nil+: * - * a = [:foo, 'bar', 2] - * a.slice!(0..-2) # => [:foo, "bar"] - * a # => [2] + * a = ['a', 'b', 'c', 'd'] + a.slice!(5..6) # => nil * * If range.start is negative, - * calculates the start index backwards from the end of the array: + * calculates the start index by counting backwards from the end of +self+: * - * a = [:foo, 'bar', 2] - * a.slice!(-2..2) # => ["bar", 2] - * a # => [:foo] + * a = ['a', 'b', 'c', 'd'] + * a.slice!(-2..2) # => ["c"] + * + * If range.end is negative, + * calculates the end index by counting backwards from the end of +self+: * + * a = ['a', 'b', 'c', 'd'] + * a.slice!(0..-2) # => ["a", "b", "c"] + * + * Related: see {Methods for Deleting}[rdoc-ref:Array@Methods+for+Deleting]. */ static VALUE