The
first() of
enumerable is an inbuilt method in
Ruby returns the first N elements or the first element of the enumerable. If there is no first element, it returns nil. If there are less than N elements, then it returns all the elements.
Syntax: enu.first(N)
Parameters: The function takes N which signifies the first N elements which is to be returned. If N is not given, then it assumes N = 1.
Return Value: It returns the first or first N elements.
Example 1:
Output:
[1, 2, 3, 4, 5, 6]
Example 2:
Output:
1
Example 3:
Output:
[1, 7, 10, 11]