We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,497 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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,

3
Contributors
3
Replies
1 Day
Discussion Span
4 Months Ago
Last Updated
8
Views
arupface
Newbie Poster
16 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Look at the answer here.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

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

arupface
Newbie Poster
16 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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

L7Sqr
Practically a Posting Shark
851 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.5073 seconds using 2.47MB