Skip to content
instance method

Array#take

(::int n) -> ::Array[Elem]
take(count) → new_array

Returns a new array containing the first count element of self (as available); count must be a non-negative numeric; does not modify self:

Example
a = ['a', 'b', 'c', 'd']
a.take(2)   # => ["a", "b"]
a.take(2.1) # => ["a", "b"]
a.take(50)  # => ["a", "b", "c", "d"]
a.take(0)   # => []

Related: see Methods for Fetching.

Ruby version changes

  1. 3.0
See also #sum #take_while #sort_by! #to_a also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0