Skip to content

Instantly share code, notes, and snippets.

@just3ws
Last active November 20, 2018 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save just3ws/64c5b0579fbf2fcb326e to your computer and use it in GitHub Desktop.
Save just3ws/64c5b0579fbf2fcb326e to your computer and use it in GitHub Desktop.
De Morgan's laws (in Ruby)
puts "<< De Morgan's laws >>\n\n"
PAIRS = [
[false, true],
[true, false],
[true, true],
[false, false]
]
puts "\"NOT (A AND B)\" is the same as \"(NOT A) OR (NOT B)\""
puts "\nYep!" if PAIRS.all? do |a, b|
puts <<-MSG
Example: (!#{a} || !#{b}) == !(#{a} && #{b})
Ruby:
Bad: if !#{a} || !#{b}
Good: unless #{a} && #{b}
MSG
(!a || !b) == !(a && b)
end
puts "\n\"NOT (A OR B)\" is the same as \"(NOT A) AND (NOT B)\"."
puts "\nYep!" if PAIRS.all? do |a, b|
puts <<-MSG
Example: !(#{a} || #{b}) == (!#{a} && !#{b})"
Ruby:
Bad: if !#{a} && !#{b}
Good: unless #{a} || #{b}
MSG
!(a || b) == (!a && !b)
end
@just3ws
Copy link
Author

just3ws commented Aug 28, 2015

The world could be a better place. https://en.wikipedia.org/wiki/De_Morgan%27s_laws

@rl-mike
Copy link

rl-mike commented Sep 21, 2016

#  Example: !(false || true) == (!false && !true)"
#    Ruby:
#      Bad:  if !false && !true
#      Good: unless false || true
#  Example: !(true || false) == (!true && !false)"
#    Ruby:
#      Bad:  if !true && !false
#      Good: unless true || false
#  Example: !(true || true) == (!true && !true)"
#    Ruby:
#      Bad:  if !true && !true
#      Good: unless true || true
#  Example: !(false || false) == (!false && !false)"
#    Ruby:
#      Bad:  if !false && !false
#      Good: unless false || false
#
# Yep!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment