From: John Joyce Date: 2007-05-05T14:05:07+09:00 Subject: Re: Why do these two code blocks have different behavior? --Apple-Mail-1-318320765 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed The return statement is causing an early break. it doesn't do what you think it does (what it does in other languages) The desired behavior is in q2. Ruby automatically returns the result of the last statement in the block. Also, a clue that you're thinking in another language is the curly braces. Ruby style is to use curly braces for one-liners and do-end for multi- line blocks. On May 5, 2007, at 1:45 PM, yuricake wrote: > require "pp" > > @@a = [1, 2, 3] > > def q1 > @@a.inject(0) {|sum, x| > pp x > return sum + x > } > end > > def q2 > @@a.inject(0) {|sum, x| > pp x > sum + x > } > end --Apple-Mail-1-318320765--