Syntax

Ruby Map an Array Syntax

In Ruby, map an array: transforms each item — nums.map { |n| n * 2 }

Quick Answer

Syntax
nums.map { |n| n * 2 }
RubySyntax

Map an Array

Code example
nums.map { |n| n * 2 }

Transforms each item.

Learn More

How to map an array in Ruby

  1. Open your Ruby file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Ruby Map an Array Syntax

Use nums.map { |n| n * 2 }.

Related Syntax References

Frequently asked questions

What is the Ruby syntax for map an array?

Use this pattern: nums.map { |n| n * 2 }

Can I copy this Ruby code example?

Yes. Use Copy code and adapt the example to your program.

Is this Ruby syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify