From: "Jesús Gabriel y Galán" Date: 2008-05-16T15:49:45+09:00 Subject: Re: Handling of arrays On Thu, May 15, 2008 at 9:45 AM, Clement Ow wrote: > Clement Ow wrote: > However, for education sake, do u mind explaining how the whole inject > statement works? thanks! ;) Enumerable#inject is a very powerful iterator (in my opinion at least). What it does is iterate over all elements in an enumerable, yielding to the block and accumulator and the next element in the enumerable. The accumulator then gets updated by the result of the block, so the next iteration will be yielded that value. If you specify a parameter to inject, that will be the first accumulator. If not, the first element of the enumerable is used instead. Some examples: irb(main):003:0> [1,2,3].inject(0) {|total,x| p [total, x]; total + x} [0, 1] [1, 2] [3, 3] => 6 irb(main):004:0> [1,2,3].inject {|total,x| p [total, x]; total + x} [1, 2] [3, 3] => 6 Another one (although this is just to show how inject works, cause the functionality would be better achieved by map): irb(main):011:0> [1,2,3,4,5].inject([]) {|total,x| p [total,x]; total + [x**2]} [[], 1] [[1], 2] [[1, 4], 3] [[1, 4, 9], 4] [[1, 4, 9, 16], 5] => [1, 4, 9, 16, 25] The p [total,x]; helps in showing what gets passed to the block each time. Just remember: the result of the block will be the next "total". In our case, the result of the block was the original array minus the files that matched the exceptions. So each time that array was injected (well, a copy) along with the next exception, and the result of the block would be another array with less elements, etc. Hope this helps, Jesus.