Related Articles

Related Articles

Ruby | Enumerable collect_concat() function
  • Last Updated : 05 Dec, 2019

The collect_concat() of enumerable is an inbuilt method in Ruby returns a new array with the concatenated results of running block once for every element in enum. In case no block is given, an enumerator is returned instead.

Syntax: block.collect_concat { |obj| block }

Parameters: The function takes the block according to which the every block is to be returned. If no block is given, an enumerator is returned.

Return Value: It returns a new array.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for collect_concat method in Enumerable
  
# Initialize
enu = [12, 18]
  
# returns enumerator
res = enu.collect_concat { |el| [2*el, 3*el] }

chevron_right


Output:

[24, 36, 36, 54]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for collect_concat method in Enumerable
  
# Initialize
enu = [[17, 21], [19, 100]]
  
# returns enumerator
res = enu.collect_concat { |el| el + [1000] }

chevron_right


Output:

[17, 21, 1000, 19, 100, 1000]
My Personal Notes arrow_drop_up
Recommended Articles
Page :