MΓ³dulo 25 Β· Blocks, Procs & Lambdas β LecciΓ³n 4 de 6 Β· ~8 min
Symbol#to_proc & Method Objects
Why map(&:name) works β finally explained
You've written walkers.map(&:name) for modules. Here's the machinery, and it's a favorite interview probe. & expects a proc; if you give it something else, Ruby calls to_proc on it first. Symbol#to_proc turns :name into the proc ->(x) { x.name }. So &:name becomes "call .name on each element."
In an interview, say: "
&:nameis&coercing the symbol viaSymbol#to_procinto->(x){ x.name }, then expanding that proc into the block. It's shorthand for{ |x| x.name }β great for single no-arg method calls, useless the moment you need arguments."