Ruby logic operator or || difference

Possible duplicate:
Ruby: difference between || and 'or'

There is no 'or' and '||' in a ruby same? I get different results when executing the code.

line =""
if (line.start_with? "[" || line.strip.empty?)
  puts "yes"
end




line =""
if (line.start_with? "[" or line.strip.empty?)
  puts "yes"
end
+5
source share
3 answers

No, both operators have the same effect, but have different priority.

The operator ||has a very high priority, so it is very strongly attached to the previous value. An operator orhas a very low priority, so it communicates less tightly than another operator.

The reason for having two versions is that it has a high priority and the other has a low priority, because it is convenient.

+8
source

|| , - - , , , :

(line.start_with? ("[" || line.strip.empty?))

(line.start_with? ("["))

FALSE

,

((line.start_with? "[") or line.strip.empty?)

(FALSE or TRUE)

true

, .: -)

+3

Daniel is right, more clearly:

if (line.start_with?("[") || line.strip.empty?)
  puts "yes"
end

will create yes

+1
source

All Articles