Skip to content

Commit 0960c8a

Browse files
[DOC] Tweaks for Array#fetch_values (#11603)
1 parent ce5fd35 commit 0960c8a

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

array.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8600,6 +8600,7 @@ rb_ary_deconstruct(VALUE ary)
86008600
*
86018601
* - #[] (aliased as #slice): Returns consecutive elements as determined by a given argument.
86028602
* - #fetch: Returns the element at a given offset.
8603+
* - #fetch_values: Returns elements at given offsets.
86038604
* - #first: Returns one or more leading elements.
86048605
* - #last: Returns one or more trailing elements.
86058606
* - #max: Returns one or more maximum-valued elements,

array.rb

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,37 @@ def last n = unspecified = true
201201
end
202202

203203
# call-seq:
204-
# array.fetch_values(*indexes) -> new_array
205-
# array.fetch_values(*indexes) {|key| ... } -> new_array
204+
# fetch_values(*indexes) -> new_array
205+
# fetch_values(*indexes) {|index| ... } -> new_array
206+
#
207+
# With no block given, returns a new array containing the elements of +self+
208+
# at the offsets given by +indexes+;
209+
# each of the +indexes+ must be an
210+
# {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects]:
206211
#
207-
# Returns a new Array containing the values associated with the given indexes *indexes:
208212
# a = [:foo, :bar, :baz]
209-
# a.fetch_values(3, 1) # => [:baz, :foo]
213+
# a.fetch_values(3, 1) # => [:baz, :foo]
214+
# a.fetch_values(3.1, 1) # => [:baz, :foo]
215+
# a.fetch_values # => []
216+
#
217+
# For a negative index, counts backwards from the end of the array:
218+
#
219+
# a.fetch_values([-2, -1]) # [:bar, :baz]
220+
#
221+
# When no block is given, raises an exception if any index is out of range.
222+
#
223+
# With a block given, for each index:
224+
#
225+
# - If the index in in range, uses an element of +self+ (as above).
226+
# - Otherwise calls, the block with the index, and uses the block's return value.
210227
#
211-
# Returns a new empty Array if no arguments given.
228+
# Example:
212229
#
213-
# When a block is given, calls the block with each missing index,
214-
# treating the block's return value as the value for that index:
215230
# a = [:foo, :bar, :baz]
216-
# values = a.fetch_values(1, 0, 42, 777) {|index| index.to_s}
217-
# values # => [:bar, :foo, "42", "777"]
231+
# a.fetch_values(1, 0, 42, 777) {|index| index.to_s}
232+
# # => [:bar, :foo, "42", "777"]
218233
#
219-
# When no block is given, raises an exception if any given key is not found.
234+
# Related: see {Methods for Fetching}[rdoc-ref:Array@Methods+for+Fetching].
220235
def fetch_values(*indexes, &block)
221236
indexes.map! { |i| fetch(i, &block) }
222237
indexes

0 commit comments

Comments
 (0)