From: Mike Harris Date: 2006-09-28T07:24:22+09:00 Subject: Re: Injecting truth Ken Kunz wrote: >I had a scenario arise today where I needed an iterator with similar >semantics as Enumerable#all?, but non-short-circuiting. We came up with >a fairly elegant solution (imho), so I thought I'd be a good rubyzen >and share :) > >1) First... the standard #all? (short-circuits): > >irb> (1..5).all? { |i| puts i; i < 3 } >1 >2 >3 >=> false > >2) Non-short-circuiting version (injecting truth): > >irb> (1..5).inject(true) { |retval, i| puts i; retval &&= (i < 3) } >1 >2 >3 >4 >5 >=> false > >3) One more way (though I like #2 better) > >irb(main):016:0> (1..5).partition { |i| puts i; i < 3 }.last.empty? >1 >2 >3 >4 >5 >=> false > >-Ken > > > > > 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.