Ruby | Enumerable map() function
The map() of enumerable is an inbuilt method in Ruby returns a new array with the results of running block once for every element in enum. The object is repeated every time for each enum. In case no object is given, it return nil for each enum.
Syntax: (r1..r2).map { |obj| block }
Parameters: The function takes the object and the block which is for every enum, it also takes r1 and r2 which decides on the number of elements in the returned enumerable.
Return Value: It returns a new array.
Example #1:
# Ruby program for map? method in Enumerable # returns enumerator enu1 = (2..6).map {|x| x * 10} |
Output:
[20, 30, 40, 50, 60]
Example #2:
# Ruby program for map? method in Enumerable # returns an enumerator with nil enu1 = (2..6).map {} |
Output:
[nil, nil, nil, nil, nil]
Recommended Posts:
- Ruby | Enumerable sum() function
- Ruby | Enumerable one? function
- Ruby | Enumerable none?() function
- Ruby | Enumerable first() function
- Ruby | Enumerable all? function
- Ruby | Enumerable min() function
- Ruby | Enumerable max() function
- Ruby | Enumerable any? function
- Ruby | Enumerable take() function
- Ruby | Enumerable collect() function
- Ruby | Enumerable minmax() function
- Ruby | Enumerable min_by() function
- Ruby | Enumerable minmax_by() function
- Ruby | Enumerable member? function
- Ruby | Enumerable include?() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


