Ruby | Enumerable first() function Last Updated : 05 Dec, 2019 Comments Improve Suggest changes Like Article Like Report 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: ruby # Ruby program for first method in Enumerable # Initialize enu = (1..10) # Prints enu.first(6) Output: [1, 2, 3, 4, 5, 6] Example 2: ruby # Ruby program for first method in Enumerable # Initialize enu = [1, 7, 10, 11] # Prints enu.first Output: 1 Example 3: javascript # Ruby program for first method in Enumerable # Initialize enu = [1, 7, 10, 11] # Prints enu.first(10) Output: [1, 7, 10, 11] Comment More infoAdvertise with us Next Article Ruby | Enumerable first() function gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Collections Ruby Enumerable-class Similar Reads Ruby | Enumerable find() function The find() of enumerable is an inbuilt method in Ruby returns the first element which satisfies the given condition in the block. If there is no block, then it returns the enumerator itself. Syntax: block.find { |obj| block } Parameters: The function takes the block according to which the first whic 1 min read Ruby | Enumerable flat_map function The flat_map() of enumerable is an inbuilt method in Ruby returns a new array with the concatenated results of running block once for every element in enum. In case no block is given, an enumerator is returned instead. Syntax: block.flat_map { |obj| block } Parameters: The function takes the block a 1 min read Ruby | Enumerable find_all() function The find_all() of enumerable is an inbuilt method in Ruby returns the items in the enumerable which satisfies the given condition in the block. It returns an enumerator if no block is given. Syntax: enu.find_all { |obj| block } Parameters: The function takes a block whose condition is used to find t 1 min read Ruby | Enumerable find_index() function The find_index() of enumerable is an inbuilt method in Ruby returns the index of the item which returns true for the given condition in the block, or the index of the item which is equal to the given value. If no block is given, then it returns an enumerator. If the values is not present in the enum 1 min read Ruby | Enumerable map() function The map() of enumerable is an inbuilt method in Ruby returns a new array with the results of running block once for every element in enum. The object is repeated every time for each enum. In case no object is given, it return nil for each enum. Syntax: (r1..r2).map { |obj| block } Parameters: The fu 1 min read Ruby | Enumerable min() function The min() of enumerable is an inbuilt method in Ruby returns the minimum elements or an array containing the minimum N elements in the enumerable. When no block is given, it assumes all elements to be self comparable, but when the block is given then it is compared using . Syntax: enu.min(n) { |a, b 1 min read Ruby | Enumerable all? function The all?() of enumerable is an inbuilt method in Ruby returns a boolean value true if all the objects in the enumerable satisfies the given condition, else it returns false. If a pattern is given, it compares with the pattern, and returns true if all of them are equal to the given pattern, else it r 2 min read Ruby | Enumerable max() function The max() of enumerable is an inbuilt method in Ruby returns the maximum elements or an array containing the maximum N elements in the enumerable. When no block is given, it assumes all elements to be self comparable, but when the block is given then it is compared using . Syntax: enu.max(n) { |a, b 1 min read Ruby | Enumerable any? function The any?() of enumerable is an inbuilt method in Ruby returns a boolean value if any of the object in the enumerable satisfies the given condition, else it returns false. Syntax enu.any? { |obj| block } or enu.any?(pattern) Parameters: The function takes two types of parameters, one is the object an 2 min read Ruby | Enumerable sum() function The sum() of enumerable is an inbuilt method in Ruby returns the sum of all the elements in the enumerable. If a block is given, the block is applied to the enumerable, then the sum is computed. If the enumerable is empty, it returns init. Syntax: enu.sum { |obj| block } Parameters: The function acc 1 min read Like