1,105,578 Community Members

Is there any difference between `inject(:+) and inject(&:+)` ?

Member Avatar
arupface
Newbie Poster
16 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

Is there any difference between inject(:+) and inject(&:+) ? If NO hen how these two kind of syntax with same method? And if Yes then why the output are same for both?

irb(main):005:0> [1, 2, 3].inject(&:+)
=> 6
irb(main):006:0> [1, 2, 3].inject(:+)
=> 6
irb(main):007:0>

Thanks,

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
0
 

Look at the answer here.

Member Avatar
arupface
Newbie Poster
16 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

That link is too heavy wieght, ltos of opinoin has been put over there. Any brief you want me to share,just the concept on the same!

Thanks

Member Avatar
L7Sqr
Veteran Poster
1,006 posts since Feb 2011
Reputation Points: 179 [?]
Q&As Helped to Solve: 169 [?]
Skill Endorsements: 11 [?]
 
0
 

From the selected answer in Taywins link:

I didn't notice the ampersand. You don't need it, inject will work with a symbol. But if you do write it, the symbol is converted to block, it can be useful with other methods

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article