[Ruby] problem with if statement

background

When I solved the Ruby problem, I was disappointed to see the smartness of the model answer for my answer: confounded :, so I will leave it as a memorandum.

problem

Enter a positive integer. That integer is The difference from multiples of 10 (10,20,30 ...) True if within 2 Other than that, let's create a method that outputs False.

** Output example: ** near_ten(12)→True near_ten(17)→False near_ten(19)→True

My answer

Ruby


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

Model answer

Ruby


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

Impressions

When I saw the model answer, I shouted in my heart that it was ** too smart! (I can't speak out because I'm on the go: unamused :)

Certainly, or" || "It is easy to forget the existence of. .. .. ..

To be honest, the code is correct: sparkles :, but I felt once again that the coolness oozes out depending on how smart: star2: is written. .. ..

Easy to refactor. .. ..

Recommended Posts

[Ruby] problem with if statement
Ruby problem ⑦
if statement
ruby search problem
[Ruby] FizzBuzz problem
ruby API problem
ruby API problem
[Competition Pro] Solve the knapsack problem with Ruby
[Ruby] What if you put each statement in each statement? ??
Getting Started with Ruby
10 Corresponds to if statement
Studying Java-Part 10-if statement
Evolve Eevee with Ruby
[At Coder] Solve the ABC183 D problem with Ruby
[At Coder] Solve the ABC182 D problem with Ruby
About for statement and if statement
Ruby version switching with rbenv
I tried DI with Ruby
GraphQL Client starting with Ruby
Ruby: Send email with Starttls
Format Ruby with VS Code
Integer check method with ruby
Let's understand the if statement!
Points for refactoring (if statement)
Studying with CodeWar (ruby) ⑤ proc
Use Ruby with Google Colab
Getting Started with Ruby Modules
Ruby deposit system, algorithm problem
[ruby] Method call with argument
I tried to solve the problem of "multi-stage selection" with Ruby
Install Ruby on MSYS2 with pacman
Ruby Scraping-Move Selenium Headless with VPS.
[Java] Branch enum with switch statement
Calendar creation problem (fun Ruby practice problem)
Programming with ruby (on the way)
Studying with CodeWar (ruby) ④ case ~ when
Handle DatePicker with Ruby / gtk3 + glade3
[Java] Reduce if statements with Enum
About if statement and branch processing
This problem is soberly difficult ... (Ruby)
Install ruby on Ubuntu 20.04 with rbenv
Make a typing game with ruby
[Beginner] Let's solve AtCoder problem with Ruby while looking at the article!
I'm still exhausted with redundant if statements! ?? Write an if statement like this
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
If you want to make a zip file with Ruby, it's rubyzip.
If you have trouble with the character code problem in Myanmar (Burmese)