From: Rick DeNatale Date: 2010-09-29T05:23:22+09:00 Subject: Re: puts and return On Tue, Sep 28, 2010 at 4:14 PM, Kirk Haines wrote: > On Tue, Sep 28, 2010 at 2:03 PM, Jim Haungs wrote: >> 10.times do |i| >>  puts 'too big' and break if i > 5 >>  puts "#{i} ok" >> end >> >> I would expect this loop to quit after printing 0 to 5, but it executes >> all 10 times. I know "puts" return nil, but isn't "and" (as opposed to >> &&) supposed to execute both sides of the expression even if one side >> evaluates to false? > > && is what you want, to get that effect. No that doesn't work, since puts returns nil and nil || whatever doesn't evaluate whatever. And puts "Too big" || break if i > 5 Doesn't cause the break. I think this is because break really isn't an expression. The best solution seems to be if i > 5 puts "Too big" break end Both because it works, and I don't put too much weight on marginally shorter code. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale