Divide by Zero High

Divide by zero is detected, this can result in exceptions, or unintended values. Either wrap the expression in an if statement checking the denominator, or wrap in an exception handling begin block.

Detector ID
ruby/divide-by-zero@v1.0
Category
Common Weakness Enumeration (CWE) external icon
Tags
-

Noncompliant example

1def divide_by_zero_noncompliant
2  zero = 0
3  # Noncompliant: divide by zero 
4  bad = variable/zero
5end

Compliant example

1def divide_by_zero_compliant
2  # Compliant: check before dividing 
3  if zero != 0
4    variable / zero
5  end
6end