From: Matthew Kerwin Date: 2012-09-13T11:30:03+09:00 Subject: Re: Breaking Down the Block `my_array.inject(0) {|t,n| t + n}` basically breaks down like: t = 0 my_array.each {|n| t = (t + n) } t Similary, leaving out the inital value, `my_array.inject {|t,n| t + n}` basically breaks down like: t = my_array[0] my_array[1..-1].each {|n| t = (t + n) } t What you called 't' (often called the 'memory') is the accumulation, or the result of the last iteration of the block. On 13 September 2012 12:09, 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? > > -- > Posted via http://www.ruby-forum.com/. > -- Matthew Kerwin, B.Sc (CompSci) (Hons) http://matthew.kerwin.net.au/ ABN: 59-013-727-651 "You'll never find a programming language that frees you from the burden of clarifying your ideas." - xkcd