From: Mike Harris Date: 2006-09-28T23:03:54+09:00 Subject: Re: Injecting truth Robert Klemme wrote: > Mike Harris wrote: > >> Also, with #2, obviously your way works, but the &&= isn't neccesary, >> a && has the intended effect, since the result of the block is put in >> the accumulator. Again, it doesn't matter at all, I'm mostly just >> writing to avoid doing work. > > > In that case order should be reversed to avoid short circuiting > altogether (in this case it does not matter as the test has no side > effects, but if it had...) > >>> (1..5).inject(true) { |retval, i| puts i; i < 3 && retval} >> > 1 > 2 > 3 > 4 > 5 > => false > > Kind regards > > robert > > > > Very good point, neglected to think about that, glossing over the fact that the puts executing doesn't mean the relevant expression evaluated. With the fixed example: irb(main):001:0> (1..5).inject(true) { |s,i| s &&= (puts i; i < 3) } 1 2 3 => false irb(main):002:0> (1..5).inject(true) { |s,i| s && (puts i; i < 3) } 1 2 3 => false irb(main):003:0> (1..5).inject(true) { |s,i| (puts i; i < 3) && s } 1 2 3 4 5 => false With the &&= vs && thing, I made a salient point about a bug in the example and I was too dumb to realize it.