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 of the initialized object as the parameter.
Returns: the constituent elements of the initialized object.
Example 1:
# Initialising a Hash objectname_age = { 'name' => 'Geek', 'age' => '22' } # Calling the each functionC = name_age.each { |key, value| print key + ' = ' + value + "\n" } # Getting the key and value of hash objectputs "#{C}" |
Output:
name = Geek
age = 22
{"name"=>"Geek", "age"=>"22"}Example 2:
# Initialising a arraystooges = ['GFG', 'gfg', 'Geeks', 'Geek'] # Calling the each functionC = stooges.each { |stooge| print stooge + "\n" } # Getting the values of the arrayputs "#{C}" |
Output:
GFG gfg Geeks Geek ["GFG", "gfg", "Geeks", "Geek"]

