Ruby | Enumerable take() function
The take() of enumerable is an inbuilt method in Ruby returns the first N elements from the enumerable. If it does not have N elements, then it returns the entire enum.
Syntax: enu.take(n)
Parameters: The function accepts N which specifies the number of elements to be returned.
Return Value: It returns the first N elements.
Example #1:
# Ruby program for take method in Enumerable # Initialize enu = [10, 13, 12, 11] # Prints enu.take(3) |
chevron_right
filter_none
Output:
[10, 13, 12]
Example #2:
# Ruby program for take method in Enumerable # Initialize enu = (1..19) # Prints enu.take(4) |
chevron_right
filter_none
Output:
[1, 2, 3, 4]
Recommended Posts:
- Ruby | Enumerable any? function
- Ruby | Enumerable map() function
- Ruby | Enumerable none?() function
- Ruby | Enumerable all? function
- Ruby | Enumerable first() function
- Ruby | Enumerable max() function
- Ruby | Enumerable min() function
- Ruby | Enumerable one? function
- Ruby | Enumerable sum() function
- Ruby | Enumerable sort_by() function
- Ruby | Enumerable each_cons() function
- Ruby | Enumerable drop() function
- Ruby | Enumerable sort() function
- Ruby | Enumerable reverse_each() function
- Ruby | Enumerable select() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


