2

I have a doubt regarding unless and if blocks in ruby language. Please excuse me if I'm that stupid to ask this question.

Lets assume we have two boolean variables a = true and b = true.

condition: I want to check whether both of them are true. Using if statement we can check that as given below.

if a == true && b == true
  print "Hai"
end

doubt: instead of the above statement, can I use the one given below?

unless a == false && b == false
  print "Hai"
end

Will both the blocks given above give the same result?

Rajesh Omanakuttan
  • 6,788
  • 7
  • 47
  • 85

6 Answers6

6

Instead of

if a == true && b == true
  print "Hai"
end

you can write

if a && b
  print "Hai"
end

This is the same as

unless !(a && b)
  print "Hai"
end

Which is (using De Morgan's laws) the same as

unless !a || !b
  print "Hai"
end
tessi
  • 13,313
  • 3
  • 38
  • 50
1

No, and it comes from De Morgan's laws.

Firstly, if you check boolean value, you don't need this weird construction a == true, it's sufficient to use

if a && b

Secondly, !(a && b) == !a || !b (this part comes from De Morgan's laws), so if you want to use unless and you want it to be equivalent to the previous if expression, you should use it this way:

unless !a || !b

Since it's quite messy, you should stay with if a && b.

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
1

if is not loop if is check condition operator:

if a == true && b == true
  print "Hai"
end

Make sure that you differ = and == operator, because = is assign operator, == is condition operator, which returns true or false results that are ruby classes. To make it as a loop do:

while true
  if a == true && b == true
    print "Hai"
  else
    break
  end
end

Which can be simpified to:

while a && b
  print "Hai"
end

unless operator is also check condition keyword as if, and it can be represented as if ! or if not. You can transform condition for if operator with the negated condition for unless operator using DeMorgan's rule. In your case it shell be as:

unless a == false || b == false # not &&
  print "Hai"
end

or if you do a loop:

while true
  unless a == false || b == false
    print "Hai"
  else
    break
  end
end

And in the simplified form:

while !( a == false || b == false )
  print "Hai"
end
Community
  • 1
  • 1
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69
0

if a == true and b == false then a == false and b == false is false.

Therefore, unless a == false and b == false is true

but b == false

So no, it's not the same :)

aherve
  • 3,795
  • 6
  • 28
  • 41
0

I you want to test out code. Best thing in ruby is to spin up an instance of irb.

The interactive ruby bash can be used to test those assumptions.

Next thing is the usage of a_boolean == true. This is not considered good style in ruby. Just write a_boolean or !a_boolean.

Another thing is that "loops" are a different thing that "conditions".

loops -> while, until, etc

conditions -> if, unless, etc

Short answer to your question. unless is the opposite of if, it is !if

phoet
  • 18,688
  • 4
  • 46
  • 74
0

thats not a loop, its a if statement or block

if a and b
   print 'hi'
end

if you to not compare bits, dont use &&. use "and" instead. also if a means the same as if a == true

unless a and b # unless ( a and b ) is the same
   print 'hi'
end

to write the meaning down its something like

if not ( a and b ) then

hope this helps.

devanand
  • 5,116
  • 2
  • 20
  • 19