From: skim Date: 2010-08-18T08:40:24+09:00 Subject: Re: Strange behavior of or condition in loop --0015175ccf4cca3365048e0d77e6 Content-Type: text/plain; charset=ISO-8859-1 and/or aren't the same as &&/||. they have lower operator precedence than latter. i recommend reading this article on the usage for both: http://avdi.org/devblog/2010/08/02/using-and-and-or-in-ruby/ On Tue, Aug 17, 2010 at 17:50, AppleII717 wrote: > I don't understand why ruby's "or" acts differently in a loop. > > This is some code I pulled out of a rails model, expanded the > assignments and simulated in IRB (1.8.7 and 1.9.2rc) > > #simple boolean assignments > #Why does this work > > dirty = false > found = true > dirty = dirty or found > puts dirty.to_s => true > > #same type of assignments in loop giving meaning to dirty and found > #But this does not > > a = ["score_method", "updated_at"] > dirty = false > a.each{|attrib| > puts dirty.to_s + " in " > found = !( /score_method|weight|critical|minimum_value/i =~ > attrib ).nil? > dirty = dirty or found > puts dirty.to_s + " out " + found.to_s > } > puts dirty.to_s => false > > #another version with parens around or > #But this does > > a = ["score_method", "updated_at"] > dirty = false > a.each{|attrib| > puts dirty.to_s + " in " > found = !( /score_method|weight|critical|minimum_value/i =~ > attrib ).nil? > dirty = (dirty or found) > puts dirty.to_s + " out " + found.to_s > } > puts dirty.to_s => true > > > I've been doing to additive boolean assignment stuff forever without > parens. Guess I just don't understand Ruby. > > --0015175ccf4cca3365048e0d77e6--