Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Enumerable#take

enum.take(n) → array

Returns first n elements from enum.

Example
a = [1, 2, 3, 4, 5, 0]
a.take(3)             #=> [1, 2, 3]
a.take(30)            #=> [1, 2, 3, 4, 5, 0]
See also #sum #take_while #sort_by #tally also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1