Is there any way to refactor this into an unless
statement?
a = false
b = true
if !a or !b
puts "hello world"
end
This doesn't seem to be equivalent
unless a or b
puts "hello world"
end
Is there any way to refactor this into an unless
statement?
a = false
b = true
if !a or !b
puts "hello world"
end
This doesn't seem to be equivalent
unless a or b
puts "hello world"
end
Negating your condition according to De Morgan's laws...
unless (a and b)
That should be:
puts "hello" unless a and b
Or
unless a and b
puts "hello"
end
As unless
is the negation of if
, you need to negate the whole condition expression (you can use De Morgan’s laws to simplify it):
!(!a or !b) ≡ !!a and !!b ≡ a and b
So:
unless a or b
puts "hello world"
end