Skip to content

Instantly share code, notes, and snippets.

@biscuitvile
Created May 21, 2014 18:46
Show Gist options
  • Select an option

  • Save biscuitvile/627c36160df0d0136ccf to your computer and use it in GitHub Desktop.

Select an option

Save biscuitvile/627c36160df0d0136ccf to your computer and use it in GitHub Desktop.
Ternary Operator
# your familiar basic if else
if pup_is_cute?
scratch_behind_ear
else
ignore_bad_breath_poodle
end
# ternary operator does the exact same thing with a ? and : like this
pup_is_cute? ? scratch_behind_ear : ignore_bad_breath_poodle
# I usually avoid the ternary but in practice it's not bad as long as
# its short and understandable like the above example
# However my bro decided to *nest* ternaries, like this
pup_is_cute? ? seems_safe ? scratch_behind_ear : continue_being_friendly : ignore_bad_breath_poodle
if pup_is_cute?
if seems_safe?
scratch_behind_ear
else
continue_being_friendly
end
else
ignore_bad_breath_poodle
end
# Your instinct as usual is correct. Forget everything about that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment