Created
January 19, 2014 16:54
-
-
Save ph3nx/8507472 to your computer and use it in GitHub Desktop.
Ruby method that checks whether a number is evenly divisible by three or not. The function uses Ruby's ternary operator, which is a shorthand for the if-else control structure. It's a best pratice to end method names with a question mark when they return boolean values (true || false).
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
def by_three? number | |
number % 3 == 0 ? true : false | |
end | |
by_three? 5 | |
# => false | |
by_three? 9 | |
# => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
number % 3 == 0
is fine and returns true or false. Why use a ternary at all?