All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more
Будь ласка, увійдіть, щоб продовжити.

What does .map(&:name) mean in Ruby?

Post cover: What does .map(&:name) mean in Ruby?
This content has been automatically translated from Ukrainian.
In Ruby, the map(&:name) construct is a shorthand for applying a method to each element of a collection. This form is used to make the code shorter and more readable. Let's take a closer look at what this expression means.
The map method is used to iterate over each element of a collection and execute a block of code for each element. A new array with the results of the block execution is returned in response.
[1, 2, 3].map { |n| n * 2 }
# => [2, 4, 6]

What does &:name mean?

This (&:name in the expression array.map(&:name)) part is used as shorthand for a block that calls the name method for each element. It is equivalent to:
array.map { |item| item.name }
That is:
# This:
array.map { |item| item.name }
# And this:
array.map(&:name)
# Performs the same action, but the second option has a shortened form

Example

class User
  attr_accessor :name

  def initialize(name)
    @name = name
  end
end

users = [User.new('Alice'), User.new('Bob'), User.new('Charlie')]
names = users.map(&:name)
# => ["Alice", "Bob", "Charlie"]

# Let's try the 'longer' version
names = users.map{ |user| user.name }
# => ["Alice", "Bob", "Charlie"]
As we can see, both options do the same thing. What about performance? Let's check the benchmarks.

Benchmarks

require 'benchmark'

class User
  attr_accessor :name

  def initialize(name)
    @name = name
  end
end

# Generating a large array of users
users = Array.new(100_000) { |i| User.new("User#{i}") }

# Benchmarking
results = Benchmark.bm do |x|
  x.report('map(&:name)') do
    users.map(&:name)
  end

  x.report('map { |user| user.name }') do
    users.map { |user| user.name }
  end
end

puts results
The result on my machine:
[#<Benchmark::Tms:0x000000010dd19ad0 @cstime=0.0, @cutime=0.0, @label="map(&:name)", @real=0.008466999977827072, @stime=0.00038199999999999346, @total=0.0077390000000007175, @utime=0.007357000000000724>,
 #<Benchmark::Tms:0x000000010dd19670 @cstime=0.0, @cutime=0.0, @label="map { |user| user.name }", @real=0.008027000352740288, @stime=0.0003069999999999462, @total=0.007962000000001468, @utime=0.007655000000001522>]
In general, the shorthand version is more optimized and faster. But in my example, the results are approximately the same. A more massive dataset is needed for the benchmark to show a difference. So by default, use the shortened (optimized version) of the map method call. I might make a separate post with benchmarks.
Like it?React
🧵

This post doesn't have any additions from the author yet.

Scope of a local variable in Ruby
Jun 3, '24 16:46

Scope of a local variable in Ruby

How does the map method work in Ruby? An overview of the method's operation with examples.
Jul 30, '24 07:33

How does the map method work in Ruby? An overview of the method's operation with examples.

May 24, '23 18:53

What are joins in Ruby on Rails and how does it work?

Dec 7, '23 08:13

Visibility control in Ruby (public, private, and protected)

What is Memoization (examples in Ruby and Ruby on Rails)?
Feb 20, '25 18:16

What is Memoization (examples in Ruby and Ruby on Rails)?

What does a dot at the beginning of a file (.gitignore, .DS_Store, .bashrc, etc.) mean?
Aug 2, '24 13:15

What does a dot at the beginning of a file (.gitignore, .DS_Store, .bashrc, etc.) mean?

What is .gitignore? What is it for and how to use it
Aug 2, '24 14:58

What is .gitignore? What is it for and how to use it

How to remove the .DS_Store file from a Git repository?
Aug 2, '24 19:34

How to remove the .DS_Store file from a Git repository?

What is an idempotent method?
Aug 21, '24 20:57

What is an idempotent method?

How to fix a Windows crash caused by CrowdStrike?
Jul 19, '24 13:53

How to fix a Windows crash caused by CrowdStrike?

What is a repository?
Aug 21, '24 21:25

What is a repository?

We are writing a demo game Drones vs Zombies (Gosu / Ruby)
Jul 12, '24 12:17

We are writing a demo game Drones vs Zombies (Gosu / Ruby)

Gosu Ruby Tutorial - пройдемось по офіційній документації
Jul 3, '24 11:50

Gosu Ruby Tutorial - пройдемось по офіційній документації

Ruby library Gosu for creating 2D games
Jun 29, '24 08:48

Ruby library Gosu for creating 2D games

How to make an empty git commit?
Jun 28, '24 08:33

How to make an empty git commit?

[Fix] extconf.rb failed during the installation of the Ruby library Gosu
Jun 27, '24 16:38

[Fix] extconf.rb failed during the installation of the Ruby library Gosu