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 in Others by Rahul
• 8,980 points
80 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 by Soham
• 8,730 points

Related Questions In Others

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
362 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
579 views
+4 votes
0 answers

What is the best way to pass CISSP Exam In first attempt?

Is CISSP Certification Worth. And What is ...READ MORE

Jun 25, 2019 in Others by Eric
• 310 points
552 views
+1 vote
1 answer

what is the difference between error and stderr in Node.js?

Error is an object created by Node.js to handle ...READ MORE

answered Jul 4, 2019 in Others by sunshine
• 1,280 points
1,028 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
641 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
564 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
623 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
818 views
0 votes
1 answer

What does "#include <iostream>" do?

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

answered Feb 8 in Others by Soham
• 8,730 points
27 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP