Ruby | Enumerable max_by() function Last Updated : 26 Jan, 2021 Comments Improve Suggest changes Like Article Like Report The max_by() of enumerable is an inbuilt method in Ruby returns an array of maximum elements which satisfies the condition of the given block. It returns an enumerator when no block is given. Syntax: enu.max_by(n) {|obj| block} Parameters: The function takes two parameters n and block. N signifies the number of max_by maximum elements and block signifies the condition. Return Value: It returns a max element or an array containing N max elements. Example 1: Ruby # Ruby program for max_by method in Enumerable # Initialize a = ["gopal", "tunday", "geeks", "classes", "linux"] # Prints p a.max_by(2) {|obj| obj.length } p a.max_by {|obj| obj.length } Output: ["classes", "tunday"] "classes" Example 2: Ruby # Ruby program for max_by method in Enumerable # Initialize a = ["gopal", "tunday", "geeks", "classes", "linux"] # Prints p a.max_by(2) p a.max_by Output: Enumerator: ["gopal", "tunday", "geeks", "classes", "linux"]:max_by(2) Enumerator: ["gopal", "tunday", "geeks", "classes", "linux"]:max_by Comment More infoAdvertise with us Next Article Ruby | Enumerable max_by() function gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Enumerable-class Similar Reads 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 minmax_by() function The minmax_by() of enumerable is an inbuilt method in Ruby returns an array containing two elements. It contains the minimum and the maximum value in the enumerable according to the condition in the block. Syntax: enu.minmax_by { |obj| block } Parameters: The function takes an optional block. Return 1 min read Ruby | Enumerable min_by() function The min_by() of enumerable is an inbuilt method in Ruby returns an array of minimum elements which satisfies the condition of the given block. It returns an enumerator when no block is given. Syntax: enu.min_by(n) { |obj| block } Parameters: The function takes two parameters n and block. N signifies 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 minmax() function The minmax() of enumerable is an inbuilt method in Ruby returns an array containing two elements. It contains the minimum and the maximum value in the enumerable. The first form assumes all objects implement Comparable whereas the second uses the block to return a b. Syntax: enu.minmax { |a, b| bloc 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 group_by() function The group_by() of enumerable is an inbuilt method in Ruby returns an hash where the groups are collectively kept as the result of the block after grouping them. In case no block is given, then an enumerator is returned. Syntax: enu.group_by { |obj| block } Parameters: The function takes an optional 1 min read Ruby | Enumerable sort_by() function The sort_by() of enumerable is an inbuilt method in Ruby sorts enum using a set of keys generated by mapping the values in enum through the given block. The returned result is not guaranteed to be stable, it is unstable when the comparison is equal. It returns an enumerator when no block is given. S 1 min read Ruby | Enumerable cycle() function The cycle() of enumerable is an inbuilt method in Ruby calls block for each element of enum repeatedly the given numbers times or forever if none or nil is given. If a negative numbers is given or the collection is empty, it does nothing. It returns nil if the loop has finished without getting inter 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 Like