-7

Recently I saw code like

i < 0 ||

I wonder what is "||" exactly mean? How to say that in english?

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
Ken Saluda
  • 31
  • 1
  • 6

2 Answers2

2

Logical "or"

A || B is true when either A is true or B is true, or when both A and B are true.

http://www.tutorialspoint.com/ruby/ruby_operators.htm

digitalextremist
  • 5,952
  • 3
  • 43
  • 62
0

The || operator is similar to the keyword or but is different from the keyword or in extremely important ways. Below are two great write-ups on the topic, comparing the two and showing you how to use either one:


The most important thing to note in what Avdi says, is that || cannot be used for flow control, whereas or can be.

For example...

a = :value
c = b || a
#de Since `b` is undefined/null, `c` will be set to `:value`

c = b || puts("Failure!") #de This will raise an exception!

c = b or puts("Failure!") #de Will set `c` to `NilClass` and output "Failure!"
digitalextremist
  • 5,952
  • 3
  • 43
  • 62