Enforce using Integer to check the type of an integer number

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.


ID: ruby-best-practices/integer-type-checking

Language: Ruby

Severity: Notice

Category: Best Practices


The rule enforces the use of the Integer class when performing type checking on integer numbers in Ruby. This is important because, in Ruby, integers can be either Fixnum or Bignum depending on their size. However, both Fixnum and Bignum are subclasses of Integer, making Integer the most appropriate class to use when checking if a number is an integer.

Using Integer for type checking increases the readability and maintainability of your code. It avoids the need to check for both Fixnum and Bignum separately, which can lead to redundant and cluttered code. Additionally, using Integer for type checking ensures that your code will continue to work correctly if Ruby changes its implementation of integer numbers in the future.

To abide by this rule and maintain good coding practices, always use Integer when checking if a number is an integer. Instead of writing num.is_a?(Fixnum) or num.is_a?(Bignum), write num.is_a?(Integer). This ensures your code is succinct, easily understandable, and robust against potential changes in Ruby’s integer implementation.

Non-Compliant Code Examples


Compliant Code Examples

https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis