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.