What does map(&:name) mean in Ruby?
map(&:name)
is Ruby’s symbol-to-proc shorthand. It’s equivalent to writing:
map { |element| element.name }
Here’s how it works:
:name
is a symbol referencing thename
method.&:name
callsto_proc
on the symbol, creating a proc that invokes thename
method on each element in the array.map
applies this proc to each element and returns a new array with the results of callingname
on each element.
If you’d like to sharpen your Ruby coding skills and system design knowledge further, consider these courses from DesignGurus.io:
- Grokking the Coding Interview: Patterns for Coding Questions – Learn essential problem-solving patterns critical for technical interviews.
- Grokking System Design Fundamentals – Perfect for mastering distributed systems and architecting large-scale Ruby applications.
For personalized guidance, check out the Coding Mock Interview or System Design Mock Interview sessions with ex-FAANG engineers.
CONTRIBUTOR
TechGrind