Created
May 21, 2014 18:46
-
-
Save biscuitvile/627c36160df0d0136ccf to your computer and use it in GitHub Desktop.
Ternary Operator
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
| # 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