[Ruby] problem with if statement


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.


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


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

Model answer


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


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
About Ruby if statement
[Ruby] conditional branch if statement
Ruby problem ⑦
if statement
ruby search problem
ruby API problem
[Ruby] FizzBuzz problem
ruby API problem
Ruby if, case
[Ruby] I tried to diet the if statement code with the ternary operator
[Competition Pro] Solve the knapsack problem with Ruby
[Ruby] What if you put each statement in each statement? ??
Install Ruby 3.0.0 with asdf
Getting Started with Ruby
10 Corresponds to if statement
Studying Java-Part 10-if statement
11th, Classification with Ruby
Ruby Learning # 16 Return Statement
Evolve Eevee with Ruby
[At Coder] Solve the ABC183 D problem with Ruby
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
[At Coder] Solve the ABC182 D problem with Ruby
About for statement and if statement
Ruby version switching with rbenv
Solve Google problems with Ruby
I tried DI with Ruby
GraphQL Client starting with Ruby
Ruby Learning # 18 If Statements (Con't)
Ruby: Send email with Starttls
Leap year judgment with Ruby
Integer check method with ruby
Let's understand the if statement!
[Ruby] Introduction to Ruby Error statement
[Ruby] Conditional bifurcation unless statement
Ruby Learning # 8 Working With String
Studying with CodeWar (ruby) ⑤ proc
Use Ruby with Google Colab
[Ruby] REPL-driven development with pry
Getting Started with Ruby Modules
Ruby deposit system, algorithm problem
[ruby] Method call with argument
Install Ruby on MSYS2 with pacman
Make electronic music with randomness with Ruby
Ruby Scraping-Move Selenium Headless with VPS.
[Java] Branch enum with switch statement
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
[Ruby] present/blank method and postfix if.
Calendar creation problem (fun Ruby practice problem)
Let's use Amazon Textract with Ruby
Programming with ruby (on the way)
Studying with CodeWar (ruby) ④ case ~ when
[Ruby] Extracting elements with slice method
[Ruby] Handle instance variables with instance methods
[Ruby] If and else problems-with operators-
Handle DatePicker with Ruby / gtk3 + glade3
[Java] Reduce if statements with Enum
Impressions of making BlackJack-cli with Ruby
About if statement and branch processing
[Ruby] Search problem using index method
This problem is soberly difficult ... (Ruby)