From: Brian Candler Date: 2010-12-28T06:36:21+09:00 Subject: Re: inject issue jzakiya wrote in post #970917: > because 'require' operates on one item at a time > whereas these are equivalent: > > (1..100).inject :+ > (1..100).inject 0, :+ > > because '+' operates on two items at a time? No. The two forms differ as follows: (1..100).inject 0, :+ --------------------- starts with accumulator of 0 then calls :+, 1 then calls :+, 2 ... then calls :+, 100 (1..100).inject :+ ------------------ starts with accumulator of 1 (i.e. the first element) then calls :+, 2 then calls :+, 3 ... then calls :+, 100 > Again, my focus in why 'inject' behaves differently under these two > examples. Inject with an initial value applies the given block to each element. Originally, this was the only form of inject available. The newer alternative, of inject without an initial value, takes the first value of the enumeration as the initial value. Hence the block is only called for the second to last values. -- Posted via http://www.ruby-forum.com/.