What does the &:symbol idiom (symbol-to-proc) do, and how does it work under the hood?
Last updated
map(&:upcase)is shorthand formap { |x| x.upcase }: calling one method on each element.The
&callsto_procon a non-Proc operand.Symbol#to_procreturns a proc that sends the symbol to its argument, which then becomes the method's block.It only works for a single argument-less method call. Anything more needs a real block.
- https://www.infoq.com/news/2008/02/to_proc-currying-ruby19/
- https://www.alexrothenberg.com/2009/02/07/investigating-how-symbol-toproc-works.html
- https://ruby-doc.org/4.0.6/Symbol.html
- https://docs.ruby-lang.org/en/master/Proc.html
- https://www.anandhakris.com/posts/blocks-procs-and-&-operator-in-ruby/
- https://medium.com/@patrykrogedu/callable-and-runnable-objects-in-ruby-920de413f8a2
- https://dev.to/baweaver/understanding-ruby-toproc-and-function-interfaces-2o1d
- https://blog.pjam.me/posts/ruby-symbol-to-proc-the-short-version/