Ruby | Enumerable each_cons() function Last Updated : 05 Dec, 2019 Comments Improve Suggest changes Like Article Like Report The each_cons() of enumerable is an inbuilt method in Ruby iterates for consecutive N elements starting from each element every time. If no block is given, it returns the enumerator. Syntax: enu.each_cons(N) { |obj| block } Parameters: The function takes the block which is used to check the condition and N which specifies the number of consecutive elements to take. Return Value: It returns the elements iterative consecutive for each element. Example 1: ruby # Ruby program for each_cons method in Enumerable # Initialize enu = (1.. 5) # returns each element enu.each_cons(2){|obj| p obj} Output: [1, 2] [2, 3] [3, 4] [4, 5] Example 2: ruby # Ruby program for each_cons method in Enumerable # Initialize enu = (1..10) # returns each element enu.each_cons(4) Output: Enumerator: 1..10:each_cons(4) Comment More infoAdvertise with us Next Article Ruby | Enumerable each_cons() function G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Collections Ruby Enumerable-class Similar Reads Ruby | Enumerable collect() function The collect() 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).collect { |obj| block } Parameters 1 min read Ruby | Enumerable count() function The count() of enumerable is an inbuilt method in Ruby returns the number of elements in the enumerable, or the number of elements that are equal to a given element, or the number of items which satisfies the condition in the given block. Syntax: block.count { |obj| block } or block.count(element) P 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 each_slice() function The each_slice() of enumerable is an inbuilt method in Ruby iterates for each range of N elements and prints them. If no block is given, then it returns the enumerator. Syntax: enu.each_slice(N) { |obj| block } Parameters: The function takes the block which is used to check the condition and N which 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 Like