Integer check method with ruby

This time, I will write how to check if the value of a variable is an integer. As an example, let a be an integer or a variable you want to see, as follows.

if a - a.to_i == 0
  puts "This is an integer"
else
  puts "This is a non-integer"
end

There is a part called a.to_i here, but in the case of a = 1.1, it means that it is converted to an integer (rounded down after the decimal point), and 1 (when calculating as a real number, only the number after the decimal point is 0) Tsuku → Now it is 1.0).

a - a.to_i = 1.1 - 1.0 = 0.1 (cannot be 0!)

Therefore, I think there is one way to see if the square root is a real number.

Recommended Posts

Integer check method with ruby
[ruby] Method call with argument
[Ruby] Extracting elements with slice method
Ruby to_s method
Overload method with Int and Integer arguments
Compare Integer with ==
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
[Ruby] initialize method
Ruby build method
Ruby accessor method
ruby map method
[Ruby] Exclude duplicate elements with the uniq method.
Check method call arguments in blocks with RSpec
Finding pi with the Monte Carlo method? (Ruby)
Ruby Learning # 30 Initialize Method
abbreviation for ruby method
Ruby Learning # 24 Exponent Method
Getting Started with Ruby
definition of ruby method
11th, Classification with Ruby
[Ruby] Method definition summary
Evolve Eevee with Ruby
Check compliance with object-oriented exercise
Ruby version switching with rbenv
Solve Google problems with Ruby
I tried DI with Ruby
GraphQL Client starting with Ruby
Ruby: Send email with Starttls
Format Ruby with VS Code
In Ruby you can define a method with any name
Integer unified into Integer in Ruby 2.4
Ruby algorithm (inject, method definition)
Check performance quickly with irb
Various method tests with MockRestServiceServer
Ruby Learning # 8 Working With String
[Ruby] Notes on gets method
[Ruby] problem with if statement
[Ruby] Method that returns truth
Studying with CodeWar (ruby) ⑤ proc
Use Ruby with Google Colab
Exciting environment check with mkmf
Conditional branch with helper method
[Ruby] REPL-driven development with pry
Getting Started with Ruby Modules
Check with Mockito whether Method was called when Map → List (when, verify)
[Java 8] Duplicate deletion (& duplicate check) with Stream
Install Ruby on MSYS2 with pacman
Make electronic music with randomness with Ruby
Ruby design pattern template method pattern memo
Simulate the simplex method with GUI
[Ruby] Method to count specific characters
Ruby Scraping-Move Selenium Headless with VPS.
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
[Ruby] present/blank method and postfix if.
Programming with ruby (on the way)
Studying with CodeWar (ruby) ④ case ~ when
[Java] Element existence check with Stream
Introduction to algorithms with java-Shakutori method
[Ruby basics] split method and to_s method