Ruby | Enumerable each_with_index() function Last Updated : 05 Dec, 2019 Comments Improve Suggest changes Like Article Like Report The each_with_index() of enumerable is an inbuilt method in Ruby hashes the items in the enumerable according to the given block. In case no block is given, then an enumerator is returned. Syntax: enu.each_with_index { |obj| block } Parameters: The function takes the block which is used to initialise the index to the individual objects. Return Value: It returns the enumerator, if no block is given, else it hashes the items. Example 1: ruby # Ruby program for each_with_index method in Enumerable # Initialize hashing = Hash.new enu = [7, 9, 10] enu.each_with_index { |item, index| hashing[item] = index } # prints hash puts hashing Output: {7=>0, 9=>1, 10=>2} Example 2: ruby # Ruby program for each_with_index method in Enumerable # Initialize hashing = Hash.new enu = [7, 9, 10] enu.each_with_index Output: Enumerator: [7, 9, 10]:each_with_index Comment More infoAdvertise with us Next Article Ruby | Enumerable each_with_index() function G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Collections Ruby Enumerable-class Similar Reads Ruby | Enumerator each_with_index function The each_with_index function in Ruby is used to Iterate over the object with its index and returns value of the given object. Syntax: A.each_with_index Here, A is the initialised object. Parameters: This function does not accept any parameters. Returns: the value of the given object. Example 1: Ruby 1 min read Ruby | Enumerable each_witth_object() function The each_with_object() of enumerable is an inbuilt method in Ruby iterates for every object and returns the initial object. It returns the enumerator, if no block is given. Syntax: enu.each_with_object(obj) { |obj| block } Parameters: The function takes the block which is used to initialise the init 1 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 | Enumerator each_with_object function The each_with_object function in Ruby is used to Iterate the given object's each element. Syntax: A.each_with_object({}) Here, A is the initialised object. Parameters: This function does not accept any parameters. Returns: the new set of values. Example 1: Ruby # Calling the .each_with_object functi 1 min read Ruby | Enumerable reverse_each() function The reverse_each() of enumerable is an inbuilt method in Ruby returns the elements of the temporary array. The temporary array contains the enumerable in reverse order. It returns an enumerator if no block is given. Syntax: enu.reverse_each { |obj| block } Parameters: The function accepts a block. R 1 min read Like