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
90 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
0 answers

What does the suspend function mean in a Kotlin Coroutine?

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

May 26 in Others by Harshitha
• 120 points
9 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
410 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
606 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
576 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
655 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
618 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
639 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
841 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
37 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