Ruby | Enumerator each_with_object function Last Updated : 06 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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 function on an array object [:gfg, :geeks, :geek].each_with_object({}) do |item, hash| # Converting the array elements into its uppercase hash[item] = item.to_s.upcase end Output: {:gfg=>"GFG", :geeks=>"GEEKS", :geek=>"GEEK"} Example 2: Ruby # Calling the .each_with_object function on a hash { foo: 2, bar: 4, jazz: 6 }.each_with_object({}) do |(key, value), hash| # Getting the square of the hash's value hash[key] = value**2 end Output: {:foo=>4, :bar=>16, :jazz=>36} Comment More infoAdvertise with us Next Article Ruby | Enumerator each_with_object function K Kanchan_Ray Follow Improve Article Tags : Ruby Ruby-Methods Ruby Enumerable-class Similar Reads 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 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 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_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::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