Ruby | Enumerable each_witth_object() function
Last Updated :
05 Dec, 2019
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 initial object.
Return Value: It returns the enumerator, if no block is given, else it returns the initial object.
Example 1:
enu = [ 7 , 9 , 10 ]
enu.each_with_object([]) { |obj, el| el << obj+ 10 }
|
Output:
[17, 19, 20]
Example 2:
enu = [ 7 , 9 , 10 ]
enu.each_with_object([])
|
Output:
Enumerator: [7, 9, 10]:each_with_object([])
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...