2

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
user2864740
  • 60,010
  • 15
  • 145
  • 220
axsuul
  • 7,370
  • 9
  • 54
  • 71

3 Answers3

5

Negating your condition according to De Morgan's laws...

unless (a and b) 
Gishu
  • 134,492
  • 47
  • 225
  • 308
3

That should be:

puts "hello" unless a and b

Or

unless a and b
   puts "hello"
end
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
0

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
Gumbo
  • 643,351
  • 109
  • 780
  • 844