if vs ternary operator(?)
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
# 1 | |
# you have to use parens => `()` | |
# otherwise Ruby will threat it as `stuff if condition` | |
puts (if 1 | |
then 2 | |
else 3 | |
end) | |
# 2 | |
puts 1 \ | |
? 2 \ | |
: 3 | |
# 3 | |
# we are repeating word: puts | |
if 1 then | |
puts 2 | |
else | |
puts 3 | |
end | |
# 4 | |
# we are using variable | |
# we won't use that variable (a) again | |
a = if 1 then | |
2 | |
else | |
3 | |
end | |
puts a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment