module Indexable(T)
Overview
A container that allows accessing elements via a numeric index.
Indexing starts at 0
. A negative index is assumed to be
relative to the end of the container: -1
indicates the last element,
-2
is the next to last element, and so on.
Types including this module are typically Array
-like types.
Stability guarantees
Several methods in Indexable
, such as #bsearch
and #cartesian_product
,
require the collection to be stable; that is, calling #each(&)
over and
over again should always yield the same elements, provided the collection is
not mutated between the calls. In particular, #each(&)
itself should not
mutate the collection throughout the loop. Stability of an Indexable
is
guaranteed if the following criteria are met:
#unsafe_fetch
and#size
do not mutate the collection#each(&)
and#each_index(&)
are not overridden
The standard library assumes that all including types of Indexable
are
always stable. It is undefined behavior to implement an Indexable
that is
not stable or only conditionally stable.
Included Modules
- Enumerable(T)
- Iterable(T)
Direct including types
Defined in:
chem/core_ext/indexable.crInstance Method Summary
-
#index!(of object, offset : Int = 0) : Int
Returns the index of the first appearance of obj in
self
starting from the given offset. -
#index!(offset : Int = 0, &block : T -> Bool) : Int
Returns the index of the first object in
self
for which the block is truthy, starting from the given offset.
Instance Method Detail
Returns the index of the first appearance of obj in self
starting from the given offset. Raises Enumerable::NotFoundError
if
obj is not in self
.
[1, 2, 3, 1, 2, 3].index!(2, offset: 2) # => 4
Returns the index of the first object in self
for which the block
is truthy, starting from the given offset. Raises
Enumerable::NotFoundError
if no match is found.
[1, 2, 3, 1, 2, 3].index!(offset: 2) { |x| x < 2 } # => 3