Search code examples
rubysyntaxsymbols

Ruby Symbols Syntax


p [1,2,3].map(&:abs)  
p [1,2,3].map(:abs)  
p [1,2,3].reduce(&:gcd)  
p [1,2,3].reduce(:gcd)  

Can someone pls explain why line 2 will throw an error, but not line 4.


Solution

  • This isn't a general syntactic thing related to Symbol or Symbol#&. It's a special case built into the implementation of Enumerable#reduce.