From: 7stud -- Date: 2008-03-04T18:10:59+09:00 Subject: Re: and statement unknown wrote: > Hi, > > I found the following statement in routing.rb of rails framework. I > haven't seen this kind of usage anywhere. Can somebody tell me what > does this statement do? I am particularly interested in knowing how > 'and' behaves here. > > result = route.recognize(path, environment) and return result > result = true and true puts result result = false and false puts result puts x = 10 result = false and x = 20 puts result puts x result = true and x = 20 puts result puts x --output:-- true false false 10 true 20 The results are due to 'short circuiting' of the conditionals. If you have this statement: x and y and x is false, then there is no way for the whole conditional to evaluate to true. As a result, there is no need to evaluate the second expression y to determine the result of the conditional--its going to be false no matter what y evaluates to, and ruby chooses not to evaluate y. The statement: result = route.recognize(path, environment) and return result is equivalent to: result = route.recognize(path, environment) if result return result end -- Posted via http://www.ruby-forum.com/.