[Ruby] if statement concept of conditional expression

I am reviewing the drill to strengthen my logical thinking. As a beginner, I would appreciate it if you could let me know if you have any questions.

problem

True if the integer entered is less than 2 from a multiple of 10. Other than that, let's create a method that outputs False.

Model answer

def near_ten(num)
  first_place = num % 10
  if first_place <= 2 || first_place >= 8
    puts "True"
  else
    puts "False"
  end
end

Commentary

Focus on the ones place value. "The difference from a multiple of 10 is within 2" means that the ones place is one of "0,1,2,8,9". Therefore, use the modulo % of the four arithmetic operations to get the ones digit, and substitute it for first_place. Andfirst_place <= 2 || first_place >= 8so「0,1,2,8,9」I am checking if it applies to.



There are other ways like this! If anyone can tell me, I would appreciate it if you could point it out! !!

Recommended Posts

[Ruby] if statement concept of conditional expression
[Ruby] conditional branch if statement
Creating a mixed conditional expression of Rails if statement and unless
About Ruby if statement
[Ruby] Behavior of evaluation of conditional expression in while
[Ruby] Conditional branching Conditional expression order
[Ruby] Conditional bifurcation unless statement
[Ruby] problem with if statement
Java study # 4 (conditional branching / if statement)
[Ruby] Conditional expression without if? Meaning of (question mark). How to use the ternary operator.
if statement
Basics of java basics ② ~ if statement and switch statement ~
Try using conditional branching other than if statement
Ruby conditional branch. if, conditional operator (ternary operator), unless, case
Basics of Ruby
[Ruby] Assign variables in conditional expressions in if statements.
[Ruby] What if you put each statement in each statement? ??
Ruby regular expression
Ruby if, case
Omission of curly braces in if statement (Java silver)
Ruby conditional branch processing
java learning (conditional expression)
definition of ruby method
Conditional branching of numbers
10 Corresponds to if statement
Studying Java-Part 10-if statement
Basics of try-with-resources statement
[Java] It seems that `0 <hoge <10` cannot be written in the conditional expression of the ʻif` statement.
Ruby Learning # 16 Return Statement
[Ruby] Creating code using the concept of classes and instances
The idea of C # (lambda expression, for statement) to chew
Java review ③ (control syntax [if / switch / while / for], conditional expression)