Last active
November 20, 2018 15:22
-
-
Save just3ws/64c5b0579fbf2fcb326e to your computer and use it in GitHub Desktop.
De Morgan's laws (in Ruby)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
# 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
The world could be a better place. https://en.wikipedia.org/wiki/De_Morgan%27s_laws