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?
Table of contentsClick link to navigate to the desired location
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 a 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 shorter appearance

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. But 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 result is approximately the same. You need to have a more massive dataset for the benchmark to show a difference. So by default, use the shorthand (optimized version) of the map method call. I might make a separate post with benchmarks.

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

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

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

meme code
meme code@memecode
How to make an empty git commit?
28 Jun 08:33

How to make an empty git commit?

meme code
meme code@memecode
Ruby library Gosu for creating 2D games
29 Jun 08:48

Ruby library Gosu for creating 2D games

meme code
meme code@memecode
Gosu Ruby Tutorial - пройдемось по офіційній документації
03 Jul 11:50

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

meme code
meme code@memecode
We are writing a demo game Drones vs Zombies (Gosu / Ruby)
12 Jul 12:17

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

meme code
meme code@memecode
How to fix a Windows crash caused by CrowdStrike?
19 Jul 13:53

How to fix a Windows crash caused by CrowdStrike?

meme code
meme code@memecode
How does the map method work in Ruby? Overview of the method's operation with examples
30 Jul 07:33

How does the map method work in Ruby? Overview of the method's operation with examples

meme code
meme code@memecode
What does a dot at the beginning of a file (.gitignore, .DS_Store, .bashrc, etc.) mean?
02 Aug 13:15

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

meme code
meme code@memecode
What is .gitignore? What is it for and how to use it
02 Aug 14:58

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

meme code
meme code@memecode
How to remove the .DS_Store file from a Git repository?
02 Aug 19:34

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

meme code
meme code@memecode
What is an idempotent method?
21 Aug 20:57

What is an idempotent method?

meme code
meme code@memecode
What is a repository?
21 Aug 21:25

What is a repository?

meme code
meme code@memecode