Ruby | Enumerator each function
Last Updated :
27 Oct, 2021
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:
name_age = { 'name' => 'Geek' , 'age' => '22' }
C = name_age. each { |key, value| print key + ' = ' + value + "\n" }
puts "#{C}"
|
Output:
name = Geek
age = 22
{"name"=>"Geek", "age"=>"22"}
Example 2:
stooges = [ 'GFG' , 'gfg' , 'Geeks' , 'Geek' ]
C = stooges. each { |stooge| print stooge + "\n" }
puts "#{C}"
|
Output:
GFG
gfg
Geeks
Geek
["GFG", "gfg", "Geeks", "Geek"]
Share your thoughts in the comments
Please Login to comment...