Ruby | Enumerator each_with_index function Last Updated : 06 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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 # Initialising an array and calling each_with_index function [5, 10, 15, 20, 25, 30].each_with_index do |num, idx| # Getting the values of the array puts "#{num}" if ((idx) % 2 == 0) puts "end of line" end end Output: 5 end of line 10 15 end of line 20 25 end of line 30 Example 2: Ruby # Initialising an array and calling each_with_index function [5, 10, 15, 20, 25, 30].each_with_index do |num, idx| # Getting the values of the array puts "#{num}" if ((idx + 1) % 2 == 0) puts "end of line" end end Output: 5 10 end of line 15 20 end of line 25 30 end of line Comment More infoAdvertise with us Next Article Ruby | Enumerator each_with_index function K Kanchan_Ray Follow Improve Article Tags : Ruby Ruby-Methods Ruby Enumerable-class Similar Reads Ruby | Enumerable each_with_index() function 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 initialis 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 | Enumerator each function The Enumerator#each function in Ruby is used to Iterates over the object according to how this Enumerator was constructed and returns the object's values. Syntax: A.each { |key, value| print key + ' = ' + value + "\n" } Here, A is the initialized object. Parameters: This function accepts constituent 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 | Enumerator::new function The new function in Ruby is used to create a new Enumerator object, which can be used as an Enumerable. Syntax: Enumerator.new Here, Enumerator is an object. Parameters: This function does not accept any parameters. Returns: the new set of values. Example 1: Ruby # Ruby program for Enumerator::new f 1 min read Like