Ruby | Enumerator each function Last Updated : 27 Oct, 2021 Comments Improve Suggest changes Like Article Like Report 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 of the initialized object as the parameter. Returns: the constituent elements of the initialized object. Example 1: Ruby # Initialising a Hash object name_age = { 'name' => 'Geek', 'age' => '22' } # Calling the each function C = name_age.each { |key, value| print key + ' = ' + value + "\n" } # Getting the key and value of hash object puts "#{C}" Output: name = Geek age = 22 {"name"=>"Geek", "age"=>"22"} Example 2: Ruby # Initialising a array stooges = ['GFG', 'gfg', 'Geeks', 'Geek'] # Calling the each function C = stooges.each { |stooge| print stooge + "\n" } # Getting the values of the array puts "#{C}" Output: GFG gfg Geeks Geek ["GFG", "gfg", "Geeks", "Geek"] Comment More infoAdvertise with us Next Article Ruby | Enumerator each function K Kanchan_Ray Follow Improve Article Tags : Ruby Ruby-Methods Ruby Enumerable-class Similar Reads 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 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 | 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 each_cons() function 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 conditio 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 Like