Question
I found the following Ruby code in a RailsCast and want to understand what this part means: map(&:name).
def tag_names
@tag_names || tags.map(&:name).join(' ')
end
What does (&:name) do when used with map in Ruby?
Short Answer
By the end of this page, you will understand why map(&:name) is a short Ruby way to call the name method on every item in a collection. You will also learn how the & operator and symbols work together, how this compares to a normal block, and when this shorthand is useful in real Ruby and Rails code.
Concept
In Ruby, map loops through a collection and returns a new array containing the result of running something on each element.
For example:
[1, 2, 3].map { |n| n * 2 }
# => [2, 4, 6]
When you write:
tags.map(&:name)
Ruby is using a shorthand for a very common pattern:
tags.map { |tag| tag.name }
What the pieces mean
:nameis a symbol representing the method namename&tells Ruby to convert that symbol into something usable as a blockmapthen calls that method on each element
So if tags is a collection of tag objects, map(&:name) means:
- take each
tag - call
tag.name
Mental Model
Think of map as a worker on an assembly line.
- The collection is a line of items moving past
mapapplies the same action to each item&:nameis like giving the worker a small instruction card that says: for each item, ask for itsname
So instead of saying:
map { |item| item.name }
you hand Ruby a shortcut card:
map(&:name)
Same job, shorter instruction.
Syntax and Examples
Basic syntax
collection.map(&:method_name)
This is shorthand for:
collection.map { |item| item.method_name }
Example 1: Extract names
users = [
OpenStruct.new(name: "Ava"),
OpenStruct.new(name: "Liam"),
OpenStruct.new(name: "Noah")
]
users.map(&:name)
# => ["Ava", "Liam", "Noah"]
This calls name on each user object.
Example 2: Convert strings to uppercase
words = ["ruby", "rails", "api"]
words.map(&:upcase)
# => ["RUBY", "RAILS", "API"]
This calls upcase on each string.
Example 3: Compare with block syntax
tags.map(&)
Step by Step Execution
Consider this code:
words = ["ruby", "rails", "api"]
result = words.map(&:upcase)
Here is what happens step by step:
-
wordsis an array with three strings:"ruby""rails""api"
-
mapstarts looping through the array. -
&:upcasetells Ruby: for each element, call theupcasemethod. -
Ruby processes each item:
"ruby".upcase→"RUBY""rails".upcase→"RAILS""api".upcase→"API"
-
mapcollects the results into a new array.
Real World Use Cases
map(&:method_name) is common when you want one value from each object in a collection.
Common practical uses
-
Rails views: get names or titles for display
@posts.map(&:title) -
Building API responses: extract IDs or fields
users.map(&:id) -
Data cleanup: transform strings
emails.map(&:downcase) -
Admin dashboards: list labels from objects
categories.map(&:name) -
Form helpers: prepare selected values
roles.map(&:name).join(", ")
Why developers like it
- short and readable for simple method calls
- avoids unnecessary block variables
- works well in chains with
map,select, and
Real Codebase Usage
In real Ruby and Rails projects, this shorthand is often used when the block only calls one method with no extra logic.
Typical patterns
Extracting attributes
user_ids = users.map(&:id)
post_titles = posts.map(&:title)
Preparing display strings
tag_list = tags.map(&:name).join(", ")
Working with associations in Rails
@article.comments.map(&:body)
@order.line_items.map(&:price)
Combining with other enumerable methods
users.select(&:active?).map(&:email)
This means:
- keep only active users
- get each user's email
When developers avoid it
If the logic is more than a simple method call, a normal block is usually clearer:
users.map { |user| "#{user.first_name} #{user.last_name}" }
Common Mistakes
1. Thinking &:name is special syntax only for map
It is not limited to map. It can be used with other methods that accept a block, such as select, reject, and any?.
users.select(&:active?)
2. Using it when the method needs arguments
This does not work for methods that require extra arguments in this form.
Broken example:
words.map(&:gsub)
Why it fails:
gsubneeds arguments&:gsubonly says "callgsub"- it does not provide the required parameters
Use a block instead:
words.map { |word| word.gsub("a", "@") }
3. Using it when the method may not exist
Broken example:
Comparisons
map(&:name) vs block syntax
| Style | Example | Best for |
|---|---|---|
| Shorthand | tags.map(&:name) | Simple single method calls |
| Block | `tags.map { | tag |
| Block with logic | `tags.map { | tag |
map vs each
| Method | Returns | Use when |
|---|---|---|
map | A new transformed array | You want changed values |
Cheat Sheet
Quick reference
Basic form
collection.map(&:method_name)
Equivalent to:
collection.map { |item| item.method_name }
Common examples
users.map(&:name)
posts.map(&:title)
words.map(&:upcase)
users.select(&:active?)
What & does
- converts an object into a block if possible
- with symbols, Ruby uses
to_proc
What :name is
- a symbol representing the method name
name
Good use cases
- calling one method on each element
- short, clear collection transformations
Avoid when
- the method needs arguments
- you need multiple steps
- the shorthand hurts readability
Related equivalent
FAQ
What does map(&:name) mean in Ruby?
It means: loop through the collection, call name on each item, and return the results in a new array.
Is map(&:name) the same as map { |x| x.name }?
Yes. For this case, they are equivalent.
What does the & do in &:name?
It tells Ruby to convert the symbol into a block-like callable object.
Why is a symbol used instead of a string?
Ruby supports converting symbols like :name into procs for this shorthand. Strings do not work the same way here.
Can I use &:name with methods other than map?
Yes. Any method that accepts a block may be able to use this style, such as select, reject, or any?.
Does map(&:name) work if the method takes arguments?
Usually no. If arguments are needed, use a normal block.
Is map(&:name) always better than a block?
Mini Project
Description
Build a small Ruby script that works with a list of objects and uses map(&:method_name) to extract data. This helps you practice the shorthand in a realistic way, similar to how Rails apps collect names, titles, or IDs from model objects.
Goal
Create a script that stores several book objects, extracts their titles with map(&:title), and prints them as a single formatted string.
Requirements
- Define a
Bookclass with atitleattribute. - Create at least three
Bookobjects. - Store the books in an array.
- Use
map(&:title)to extract all titles. - Join the titles into one string and print the result.
Keep learning
Related questions
Calling an Overridden Monkey-Patched Method in Ruby
Learn how to call the original method when monkey patching in Ruby, including alias_method patterns, examples, pitfalls, and practical usage.
Difference Between require and include in Ruby
Learn the difference between require and include in Ruby, when to load a file, and when to mix module methods into a class.
Fixing Ruby Gem Native Extension Errors: mkmf.rb Can't Find Header Files for Ruby
Learn why Ruby gem installs fail with missing ruby.h, how native extensions work, and how to fix header file errors on Linux servers.