Skip to content

Commit 6a3eb38

Browse files
BurdetteLamarpeterzhu2118
authored andcommitted
[DOC] Tweaks for array indexes
1 parent 071d0ec commit 6a3eb38

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

array.c

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8220,43 +8220,50 @@ rb_ary_deconstruct(VALUE ary)
82208220
}
82218221

82228222
/*
8223-
* An +Array+ is an ordered, integer-indexed collection of objects, called _elements_.
8223+
* An array is an ordered, integer-indexed collection of objects, called _elements_.
82248224
* Any object (even another array) may be an array element,
82258225
* and an array can contain objects of different types.
82268226
*
8227-
* == +Array+ Indexes
8227+
* == \Array Indexes
82288228
*
8229-
* +Array+ indexing starts at 0, as in C or Java.
8229+
* \Array indexing begins at zero, as in C or Java.
82308230
*
8231-
* A positive index is an offset from the first element:
8231+
* A non-negative index is an offset from the beginning of the array:
82328232
*
8233-
* - Index 0 indicates the first element.
8234-
* - Index 1 indicates the second element.
8235-
* - ...
8233+
* a = ['a', 'b', 'c', 'd']
8234+
* a[0] # => "a"
8235+
* a[1] # => "b"
82368236
*
82378237
* A negative index is an offset, backwards, from the end of the array:
82388238
*
8239-
* - Index -1 indicates the last element.
8240-
* - Index -2 indicates the next-to-last element.
8241-
* - ...
8239+
* a[-1] # => "d"
8240+
* a[-2] # => "c"
8241+
*
8242+
* === \Range of an \Array
8243+
*
8244+
* A non-negative index is <i>in-range</i> if it is smaller than the size of the array,
8245+
* <i>out-of-range</i> otherwise:
82428246
*
8243-
* A non-negative index is <i>in range</i> if and only if it is smaller than
8244-
* the size of the array. For a 3-element array:
8247+
* a.size # => 4
8248+
* a[3] # => "d"
8249+
* a[4] # => nil
82458250
*
8246-
* - Indexes 0 through 2 are in range.
8247-
* - Index 3 is out of range.
8251+
* A negative index is <i>in-range</i> if its absolute value is
8252+
* not larger than the size of the array,
8253+
* <i>out-of-range</i> otherwise:
82488254
*
8249-
* A negative index is <i>in range</i> if and only if its absolute value is
8250-
* not larger than the size of the array. For a 3-element array:
8255+
* a[-4] # => "a"
8256+
* a[-5] # => nil
82518257
*
8252-
* - Indexes -1 through -3 are in range.
8253-
* - Index -4 is out of range.
8258+
* === Effective Index
82548259
*
82558260
* Although the effective index into an array is always an integer,
82568261
* some methods (both within and outside of class +Array+)
8257-
* accept one or more non-integer arguments that are
8258-
* {integer-convertible objects}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects].
8262+
* accept non-integer arguments that are
8263+
* {integer-convertible objects}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects]:
82598264
*
8265+
* a[1.9] # => "b"
8266+
* a[-1.9] # => "d"
82608267
*
82618268
* == Creating Arrays
82628269
*

0 commit comments

Comments
 (0)