-1

Could someone please explain to me the differences of this two following lines of code?

puts false or true or false or false or false

I don't understand why the result of the execution of that line suppose to be different of:

puts false || true || false || false || false

I would be grateful if someone could explain that to me.

Rafael Carício
  • 100
  • 2
  • 4

2 Answers2

2
p false or true #=> false => same as  (p false) or true
p false || true #=> true  => same as  p (false or true)
hirolau
  • 13,451
  • 8
  • 35
  • 47
0

Ruby have inherited some of its control flow from Perl. So in Ruby and and or are for control flow, and && and || are boolean operators. This also means that and and or have higher precedence than && and ||

Ex:

and is used like:

 (true) and puts `true`

which is equivalent to

if true then
  puts "true"
end

and && should by used like:

true && false

which is false.

jbr
  • 6,198
  • 3
  • 30
  • 42