Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Array#take

array.take(n) → new_array

Returns a new Array containing the first n element of self, where n is a non-negative Integer; does not modify self.

Examples:

Example
a = [0, 1, 2, 3, 4, 5]
a.take(1) # => [0]
a.take(2) # => [0, 1]
a.take(50) # => [0, 1, 2, 3, 4, 5]
a # => [0, 1, 2, 3, 4, 5]

Ruby version changes

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