Given that Enumerable#inject
can take either a symbol or a block as the method to be used in the iteration, as explained in an answer to this question, is there any reason to use the &
in conjunction with Symbol#to_proc
within Enumerable#inject
? The following pairs return the same result:
[1, 2, 3, 4, 5].inject(:+)
[1, 2, 3, 4, 5].inject(&:+)
[:a, :b, :c].inject({a: {b: {c: 1}}}, :fetch)
[:a, :b, :c].inject({a: {b: {c: 1}}}, &:fetch)
Is there any use case where using a symbol and using a block (created by &
) have different results? Are there any cases where one can be used and not the other?