What does map(&:name) do in this Ruby code?

events.map(&:name)

is exactly equivalent to

events.map{|x| x.name}

it is just convenient syntactic sugar.

For more details, check out the Symbol#to_proc method here. Here, :name is being coerced to a proc.

By the way, this comes up often here – it’s just very hard to google or otherwise search for ‘the colon thing with an ampersand’ :).

Leave a Comment