← homeProgramming (Програмування)

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

What does .map(&:name) mean in Ruby? Examples of using the shorthand for the map call.

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 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.

🔥 More posts

All posts
How to make an empty git commit?
Programming (Програмування)Jun 28, '24 08:33

How to make an empty git commit?

How to make an empty git commit? Let's make an empty git commit. Why? Who knows, everyone has the...

Ruby library Gosu for creating 2D games
Programming (Програмування)Jun 29, '24 08:48

Ruby library Gosu for creating 2D games

Ruby library Gosu for creating 2D games. Description of the library for game development in Ruby ...

What is an idempotent method?
Programming (Програмування)Aug 21, '24 20:57

What is an idempotent method?

What is an idempotent method? Description and examples of idempotent methods in programming.

What is a repository?
Programming (Програмування)Aug 21, '24 21:25

What is a repository?

What is a repository? What is it for and how does it work?