Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Ruby Dictionary

  1. Home
  2. Ruby Dictionary
  3. Numeric.even? / odd? / zero? / between? / clamp

Numeric.even? / odd? / zero? / between? / clamp

These are predicate methods available on integers and numeric values. They simplify conditional expressions for checking even/odd, zero, sign, and range.

Syntax

# Returns true if the integer is even.
integer.even?

# Returns true if the integer is odd.
integer.odd?

# Returns true if the value is zero.
number.zero?

# Returns true if the value is positive.
number.positive?

# Returns true if the value is negative.
number.negative?

# Returns true if the value is between min and max (inclusive).
number.between?(min, max)

# Clamps the value to the range min..max.
number.clamp(min, max)

# Returns the infinity state of a Float.
float.infinite?

# Returns true if the Float is NaN (Not a Number).
float.nan?

Method List

MethodDescription
even?Returns true if the integer is even, false if odd. Available on integers only.
odd?Returns true if the integer is odd, false if even. Available on integers only.
zero?Returns true if the value is zero. Works for both integers and floats.
positive?Returns true if the value is positive (zero is not included).
negative?Returns true if the value is negative (zero is not included).
between?(min, max)Returns true if the value is greater than or equal to min and less than or equal to max.
clamp(min, max)Returns the value clamped to the range min..max. If out of range, returns the nearest boundary value.
infinite?Returns 1 for positive infinity, -1 for negative infinity, or nil otherwise. Float only.
nan?Returns true if the value is NaN (Not a Number). Float only.

Sample Code

# even? / odd?
puts 4.even?   # true
puts 7.odd?    # true
puts 0.even?   # true (0 is even)

# zero? / positive? / negative?
puts 0.zero?       # true
puts (-5).negative?  # true
puts 3.positive?   # true

# between?: Check if a value is within a range.
puts 5.between?(1, 10)   # true
puts 15.between?(1, 10)  # false

# clamp: Restrict a value to a range.
puts 5.clamp(1, 10)   # 5 (already within range)
puts 0.clamp(1, 10)   # 1 (clamped to minimum)
puts 15.clamp(1, 10)  # 10 (clamped to maximum)

# Practical example: Managing player HP (clamped to 0..100).
hp = 120
hp = hp.clamp(0, 100)
puts "HP: #{hp}"  # HP: 100

# infinite? / nan?: Float-specific state checks.
puts (1.0 / 0).infinite?   # 1 (positive infinity)
puts (-1.0 / 0).infinite?  # -1 (negative infinity)
puts (0.0 / 0).nan?        # true (NaN)

Notes

By Ruby convention, methods that return a boolean value end with ?. Using these methods makes conditional expressions read almost like natural English (e.g., if n.even?).

clamp is especially useful whenever you need to keep a value within a specific range — such as adjusting slider UI values or managing game status values. Since Ruby 2.7, you can also pass a Range object as the argument (e.g., n.clamp(1..10)).

even? and odd? are only available on integers (Integer). Calling them on a float raises a NoMethodError. If the type is uncertain, convert the value first using Integer(n) or similar.

For iterating over integers, see number.times / upto / downto. For rounding and absolute value, see number.abs / round / ceil / floor.

If you find any errors or copyright issues, please .