Logo

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 the name method.
  • &:name calls to_proc on the symbol, creating a proc that invokes the name method on each element in the array.
  • map applies this proc to each element and returns a new array with the results of calling name on each element.

If you’d like to sharpen your Ruby coding skills and system design knowledge further, consider these courses from DesignGurus.io:

For personalized guidance, check out the Coding Mock Interview or System Design Mock Interview sessions with ex-FAANG engineers.

CONTRIBUTOR
TechGrind