From: Robert Klemme Date: 2012-01-17T05:15:56+09:00 Subject: Re: uniq with count; better way? Please do not top post. On Mon, Jan 16, 2012 at 7:08 PM, Sigurd wrote: > it seems not quite accurate to me because of block. inject uses convention that the last statement in method is a return. ??? Inject uses the convention that the block passes the "aggregator" on which it has received as first argument. This does not have anything to do with "return". > The nature of inject is to assign the last value to the memo that has not been used ever in your case. What "memo"? >  Therefore it's more natural to use short inject method definitions: either a.inject(5, :+) either 5 + a.inject(:+).  If the memo return in proc would be unnatural the inject won't pass it to the proc explicitly. ??? > On the other side I'm not a proponent of the crazy injects that could be barely understood. I think in this case inject could be used easily as well as the other solutions provided. > > On Jan 16, 2012, at 6:14 PM, Adam Prescott wrote: > >> On Jan 16, 2012 4:09 PM, "Sigurd" wrote: >>> >>> You example is not accurate though: >>> >>> 5 +  [1, 2, 3, 4].reduce(&:+) Can you please again explain what is not accurate about Adam's piece of code? Are you aware of the two different behaviors of #inject? irb(main):001:0> [[1,2,3],[1],[]].each do |a| irb(main):002:1* p a, a.inject(&:+), a.inject(0, &:+) irb(main):003:1> end [1, 2, 3] 6 6 [1] 1 1 [] nil 0 => [[1, 2, 3], [1], []] Both have their use and it depends on the use case which you pick. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/