RubyGuides
Share this post!

Ruby Map Method (With Examples)

Map is a Ruby method that you can use with Arrays, Hashes & Ranges.

The main use for map is to TRANSFORM data.

For example:

Given an array of strings, you could go over every string & make every character UPPERCASE.

Or if you have a list of User objects…

You could convert them into a list of their corresponding email addresses, phone number, or any other attribute defined on the User class.

Let’s see exactly how to do this!

Ruby Map Syntax

The syntax for map looks like this:

array = ["a", "b", "c"]

array.map { |string| string.upcase }

# ["A", "B", "C"]

First, you have an array, but it could also be a hash, or a range.

Then you call map with a block.

The block is this thing between brackets { ... }. Inside the block you say HOW you want to transform every element in the array. It’s basically a function.

What happens after you call map?

Map returns a new array with the results.

It won’t change the original.

If you want to change the original array you can use map!.

Ruby Map Examples

Here are some examples that you may find useful.

Doubling numbers:

array = [1,2,3]

array.map { |n| n * 2 }
# [2, 4, 6]

Convert strings to integers:

array = ["11", "21", "5"]

array.map { |str| str.to_i }
# [11, 21, 5]

Convert hash values to symbols:

hash = { bacon: "protein", apple: "fruit" }

hash.map { |k,v| [k, v.to_sym] }.to_h
# {:bacon=>:protein, :apple=>:fruit}

About this hash example:

You’ll find that we have two arguments instead of one, that’s because a hash element is composed of a key & a value.

Then I’m returning a new array with the transformed key & values.

The last step is to convert this back into a hash.

Ruby Map vs Each

What is the difference between map & each?

Each is like a more primitive version of map

It gives you every element so you can work with it, but it doesn’t collect the results.

Each always returns the original, unchanged object.

While map does the same thing, but…

It returns a new array with the transformed elements.

Example:

array.each { |n| n * 2 }
# [1, 2, 3]

array.map { |n| n * 2 }
# [2, 4, 6]

Ruby Map vs Collect

Map and Collect are exactly the same method.

They are different names for the same thing!

Which one should you use?

If you read open-source projects you’ll find that the most common version is map.

Use that.

How to Use Map With an Index

If you need an index with your values you can use the with_index method.

Here’s an example:

array = %w(a b c)

array.map.with_index { |ch, idx| [ch, idx] }

# [["a", 0], ["b", 1], ["c", 2]]

Bonus tip:

You can pass a parameter to with_index if you don’t want to start from index 0.

Ruby Map Shorthand (map &)

You can use a shorthand version for map when you’re calling a method that doesn’t need any arguments.

Example:

["11", "21", "5"].map(&:to_i)

Example:

["orange", "apple", "banana"].map(&:class)

This & syntax is not limited to map, it can also be used with other enumerable methods.

Map Method Mindmap

Video Tutorial

Summary

You’ve learned about the Ruby map method & how to use it! You’ve also learned about the differences between each, map & collect.

If you liked this article please share it with your Ruby friends 🙂

Thanks for reading!

Leave a Comment: