What does the map method do in Ruby

0 votes

I am new to the concept of programming. Can someone please explain what .map would do in:

params = (0...param_count).map

Feb 8, 2022 in Others by Rahul
• 9,680 points
955 views

1 answer to this question.

0 votes

The .map method will take an object and a block and will run the block for each element by outputting each of the values which are returned from the block. This will ensure that the original object will remain unchanged until the map is used.

[1, 2, 3].map { |n| n * n } #=> [1, 4, 9]

The Array and the Range are enumerable types.map with a block returning an Array. map! mutates the original array. In order to know where this is helpful and what is the difference between map! and each, an example has been shown below:

names = ['danil', 'edmund'] 

# here we map one array to another, convert each element by some rule 
names.map! {|name| name.capitalize } # now names contains ['Danil', 'Edmund'] 
names.each { |name| puts name + ' is a programmer' } # here we just do something with each element


The following output:

Danil is a programmer
Edmund is a programmer

answered Feb 8, 2022 by Soham
• 9,700 points

Related Questions In Others

0 votes
0 answers

What does the suspend function mean in a Kotlin Coroutine?

I'm reading Kotlin Coroutine and know that ...READ MORE

May 26, 2022 in Others by Harshitha
• 120 points
300 views
0 votes
1 answer

What does the " @ " symbol mean in Excel formula (outside a table)

Excel has recently introduced a huge feature ...READ MORE

answered Nov 26, 2022 in Others by narikkadan
• 63,720 points
1,161 views
0 votes
1 answer

What are the file permissions in Linux?

There are 3 types of permissions in ...READ MORE

answered Mar 8, 2019 in Others by Naba
2,053 views
0 votes
1 answer

What are the modes in VI editor?

VI editor works basically on three modes, ...READ MORE

answered Mar 8, 2019 in Others by Nabarupa
1,213 views
0 votes
1 answer

Getting error in Hadoop Streaming: Type mismatch in Key from Map

In Hadoop streaming you have to customize ...READ MORE

answered Apr 18, 2018 in Big Data Hadoop by coldcode
• 2,080 points
1,087 views
0 votes
1 answer

How to set the number of Map & Reduce tasks?

The map tasks created for a job ...READ MORE

answered Apr 18, 2018 in Big Data Hadoop by Shubham
• 13,490 points
1,712 views
0 votes
1 answer

Map and Reduce task memory settings in Hadoop YARN

It's preferable and generally, it is recommended ...READ MORE

answered Apr 18, 2018 in Big Data Hadoop by kurt_cobain
• 9,390 points
1,079 views
0 votes
1 answer

Setting the Number of Map tasks and Reduce Tasks not Working

The command that you are running is ...READ MORE

answered May 4, 2018 in Big Data Hadoop by Ashish
• 2,650 points
1,318 views
0 votes
1 answer

What does "#include <iostream>" do?

 In order to read or write the ...READ MORE

answered Feb 8, 2022 in Others by Soham
• 9,700 points
475 views
0 votes
1 answer

The uploaded file exceeds the upload_max_filesize directive in php.ini error while uploading plugin

After looking at your ERROR, it seems ...READ MORE

answered Feb 8, 2022 in Others by Soham
• 9,700 points
585 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP