Skip to content

Instantly share code, notes, and snippets.

@ph3nx
Created January 19, 2014 16:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ph3nx/8507472 to your computer and use it in GitHub Desktop.
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).
def by_three? number
number % 3 == 0 ? true : false
end
by_three? 5
# => false
by_three? 9
# => true
@dbwest
Copy link

dbwest commented Jan 25, 2018

number % 3 == 0 is fine and returns true or false. Why use a ternary at all?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment