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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for map? method in Enumerable
  
# returns enumerator
enu1 = (2..6).map {|x| x * 10}

chevron_right


Output:

[20, 30, 40, 50, 60]

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for map? method in Enumerable
  
# returns an enumerator with nil
enu1 = (2..6).map {}

chevron_right


Output:

[nil, nil, nil, nil, nil]


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.