From: matt@... (Matt Neuburg) Date: 2012-09-13T12:50:21+09:00 Subject: Re: Breaking Down the Block Think of it this way: inject(memo) { | memo, enum | block sets memo } => memo So memo has three stages: 1. Before the first iteration, memo is set to the value of the parameter (in parentheses after "inject"). 2. After each iteration, the result of the block becomes the new value of memo. 3. When it's all over, the value of memo is the result. So it's all about accumulating results into memo. (It is sometimes called the accumulator.) In your example, the accumulation involves adding the successive values of enum to the accumulator. The successive values of enum are the values of the array. So the result is the sum of the array. (It's the sum of the array plus the initial value of the memo, but that initial value is zero, so it really is just the sum.) m. incag neato wrote: > Can someone please explain in plain english how this block treats the > given variables (and values in the array)? I am struggling to understand > the relationship of 't' and 'n' with the array. > > class Array > def sum > inject(0) {|t,n| t + n} > end > end > puts [1,2,3].sum > > Is 't' effectively labeled as '0' (zero), given the assignment on the > inject method? > What "is" 't' exactly? -- matt neuburg, phd = matt@tidbits.com A fool + a tool + an autorelease pool = cool! AppleScript: the Definitive Guide - Second Edition! http://www.tidbits.com/matt/default.html#applescriptthings