Created
March 25, 2015 06:31
-
-
Save phlipper/1bce0749b118f9a8c949 to your computer and use it in GitHub Desktop.
TECH603 Day 1 Example - "Truthy" and "Falsey"
This file contains 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
# output the values | |
puts true | |
puts false | |
# test true | |
if true then | |
puts "true is true" | |
else | |
puts "true is not true" | |
end | |
# test false | |
if false | |
puts "false is not false" | |
else | |
puts "false is false" | |
end | |
# test not false | |
if !false | |
puts "the condition is not false" | |
else | |
puts "the condition is false" | |
end | |
# test equal | |
if 1 == 1 | |
puts "one equals one" | |
else | |
puts "one does not equal one" | |
end | |
# test not equal | |
if 1 == 2 | |
puts "one equals two" | |
else | |
puts "one does not equal two" | |
end | |
# test string equality | |
if "one" == "one" | |
puts "one equals one" | |
else | |
puts "one does not equal one" | |
end | |
# test result of comparison | |
if 1 < 2 | |
puts "one less than two" | |
else | |
puts "one NOT less than two" | |
end | |
# test zero | |
if 0 | |
puts "0 is true" | |
else | |
puts "0 is false" | |
end | |
# test one | |
if 1 | |
puts "1 is true" | |
else | |
puts "1 is false" | |
end | |
# test string | |
if "thing" | |
puts "string thing is true" | |
else | |
puts "string thing is false" | |
end | |
# test nil | |
if nil | |
puts "nil is true" | |
else | |
puts "nil is false" | |
end | |
# test empty string | |
if "" | |
puts "empty string is true" | |
else | |
puts "empty string is false" | |
end | |
# test regular expression match | |
if "string" =~ /ring/ | |
puts "string string contains ring" | |
else | |
puts "string string does not contain ring" | |
end | |
# test regular expression non-match | |
if "string" =~ /grin/ | |
puts "string string contains grin" | |
else | |
puts "string string does not contain grin" | |
end | |
# test empty array | |
if [] | |
puts "empty array is true" | |
else | |
puts "empty array is false" | |
end | |
# test match with in-line assignment | |
if position = ("string" =~ /ring/) | |
puts "position is #{position}" | |
else | |
puts "no position found" | |
end | |
puts "position is still #{position}" | |
# test with assignment of the return value from the conditional | |
message = if true | |
"true message" | |
else | |
"false message" | |
end | |
# output the message | |
puts message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment