@@ -201,22 +201,37 @@ def last n = unspecified = true
201
201
end
202
202
203
203
# 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]:
206
211
#
207
- # Returns a new Array containing the values associated with the given indexes *indexes:
208
212
# 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.
210
227
#
211
- # Returns a new empty Array if no arguments given.
228
+ # Example:
212
229
#
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:
215
230
# 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"]
218
233
#
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] .
220
235
def fetch_values ( *indexes , &block )
221
236
indexes . map! { |i | fetch ( i , &block ) }
222
237
indexes
0 commit comments