Skip to content

Commit 91bb6af

Browse files
[DOC] Tweaks for Array.new (ruby#11259)
1 parent b44a154 commit 91bb6af

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

array.c

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,48 +1060,46 @@ rb_ary_s_new(int argc, VALUE *argv, VALUE klass)
10601060
* call-seq:
10611061
* Array.new -> new_empty_array
10621062
* Array.new(array) -> new_array
1063-
* Array.new(size) -> new_array
1064-
* Array.new(size, default_value) -> new_array
1065-
* Array.new(size) {|index| ... } -> new_array
1063+
* Array.new(size, default_value = nil) -> new_array
1064+
* Array.new(size = 0) {|index| ... } -> new_array
10661065
*
1067-
* Returns a new +Array+.
1066+
* Returns a new +Array+ object.
10681067
*
1069-
* With no block and no arguments, returns a new empty +Array+ object.
1068+
* - With no block given:
10701069
*
1071-
* With no block and a single +Array+ argument +array+,
1072-
* returns a new +Array+ formed from +array+:
1070+
* - With no argument given,
1071+
* returns a new empty array:
10731072
*
1074-
* a = Array.new([:foo, 'bar', 2])
1075-
* a.class # => Array
1076-
* a # => [:foo, "bar", 2]
1073+
* Array.new # => []
10771074
*
1078-
* With no block and a single Integer argument +size+,
1079-
* returns a new +Array+ of the given size
1080-
* whose elements are all +nil+:
1075+
* - With argument +array+ given,
1076+
* returns a new array containing the elements of +array+:
10811077
*
1082-
* a = Array.new(3)
1083-
* a # => [nil, nil, nil]
1078+
* Array.new([:foo, 'bar', 2]) # => [:foo, "bar", 2]
10841079
*
1085-
* With no block and arguments +size+ and +default_value+,
1086-
* returns an +Array+ of the given size;
1087-
* each element is that same +default_value+:
1080+
* - With numeric argument +size+ given,
1081+
* returns a new array containing +size+ instances of the given +default_value+;
1082+
* all elements are the _same_ object +default_value+:
10881083
*
1089-
* a = Array.new(3, 'x')
1090-
* a # => ['x', 'x', 'x']
1084+
* Array.new(3) # => [nil, nil, nil]
1085+
* Array.new(0) # => []
1086+
* o = Object.new # => #<Object:0x0000013a6534a170>
1087+
* Array.new(2, o) # => [#<Object:0x0000013a6534a170>, #<Object:0x0000013a6534a170>]
1088+
* Array.new(0, o) # => []
1089+
* Array.new(-1) # Raises ArgumentError (negative array size).
10911090
*
1092-
* With a block and argument +size+,
1093-
* returns an +Array+ of the given size;
1094-
* the block is called with each successive integer +index+;
1095-
* the element for that +index+ is the return value from the block:
1091+
* - With a block given,
1092+
* returns an array of the given +size+;
1093+
* calls the block with each +index+ in the range <tt>(0..size-1)</tt>;
1094+
* the element at that +index+ is the return value from the block:
10961095
*
1097-
* a = Array.new(3) {|index| "Element #{index}" }
1098-
* a # => ["Element 0", "Element 1", "Element 2"]
1096+
* Array.new(3) {|index| "Element #{index}" } # => ["Element 0", "Element 1", "Element 2"]
1097+
* Array.new(0) {|index| "Element #{index}" } # => []
1098+
* Array.new(-1) {|index| "Element #{index}" } # Raises ArgumentError (negative array size).
10991099
*
1100-
* Raises ArgumentError if +size+ is negative.
1100+
* Raises TypeError if the argument is not either an array
1101+
* or an {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects]).
11011102
*
1102-
* With a block and no argument,
1103-
* or a single argument +0+,
1104-
* ignores the block and returns a new empty +Array+.
11051103
*/
11061104

11071105
static VALUE

0 commit comments

Comments
 (0)