All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

How &:to_s works in Ruby and what Symbol#to_proc is

Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
In Ruby, a shorthand notation for iterations is often encountered:
[1, 2, 3].map(&:to_s)
This is the same as:
[1, 2, 3].map { |n| n.to_s }
But how exactly does the &:to_s construct work?

Symbol#to_proc

The key idea is that Ruby allows converting symbols to blocks using the to_proc method.
  • The symbol :to_s represents the name of the method.
  • The to_proc method converts the symbol to a block that calls the corresponding method on each element of the collection.
  • The & operator tells Ruby that this block should be passed to a method (map, each, etc.).
This can be seen like this:
s = :to_s
p = s.to_proc
[1, 2, 3].map(&p)  # => ["1", "2", "3"]
That is, &:to_s is a shorthand for &(:to_s.to_proc)
Simply put:
When we write &:to_s, it takes each element of the array and calls the to_s method on it.
So this notation:
[1, 2, 3].map(&:to_s)
means the same as:
[1, 2, 3].map { |n| n.to_s }
&:to_s is just a short way to say "call the to_s method on each element." Ruby "expands" this symbol into a regular block.

Examples

1. Converting numbers to strings
numbers = [1, 2, 3]
strings = numbers.map(&:to_s)
# => ["1", "2", "3"]
2. Calling other methods
words = ["hello", "world"]
capitalized = words.map(&:upcase)
# => ["HELLO", "WORLD"]
3. Calling object’s own methods
class Person
  attr_reader :name
  def initialize(name)
    @name = name
  end
end

people = [Person.new("Alice"), Person.new("Bob")]
names = people.map(&:name)
# => ["Alice", "Bob"]
As AI likes to write - Conclusion:
  • &:method_name is a shorthand for &(:method_name.to_proc).
  • Ruby converts a symbol to a block using Symbol#to_proc, which calls the method on each element of the collection.
  • This makes the code shorter and more readable.
The &:to_s or &:name construct is very convenient for collections when you need to call one method on all elements.

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

How arrays work in Ruby: practical examples of each, map, select, inject, reduce, filter_map
28 Oct 10:38

How arrays work in Ruby: practical examples of each, map, select, inject, reduce, filter_map

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
How self, protected, and private work (Ruby)
28 Oct 13:52

How self, protected, and private work (Ruby)

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Integer division in Ruby: why 6 / 4 equals 1
28 Oct 14:10

Integer division in Ruby: why 6 / 4 equals 1

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What are Proc and Lambda in Ruby?
28 Oct 15:57

What are Proc and Lambda in Ruby?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What happens if you call [1, 2, 3].map(&Person)
29 Oct 17:54

What happens if you call [1, 2, 3].map(&Person)

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Singleton class (eigenclass) in Ruby: what it is and why it is needed
29 Oct 18:29

Singleton class (eigenclass) in Ruby: what it is and why it is needed

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
==, equal?, eql?, === in Ruby: what they check and when to use them
29 Oct 20:47

==, equal?, eql?, === in Ruby: what they check and when to use them

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Include, Extend, Prepend in Ruby: how they work and what the difference is
29 Oct 21:20

Include, Extend, Prepend in Ruby: how they work and what the difference is

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
module_function in Ruby: when module methods are available as module methods and as functions
29 Oct 21:53

module_function in Ruby: when module methods are available as module methods and as functions

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska