Module: ActiveSupport::CoreExtensions::Integer::EvenOdd

Included in:
Integer
Defined in:
lib/active_support/core_ext/integer/even_odd.rb

Overview

For checking if a fixnum is even or odd.

2.even?  # => true
2.odd?   # => false
1.even?  # => false
1.odd?   # => true
0.even?  # => true
0.odd?   # => false
-1.even? # => false
-1.odd?  # => true

Instance Method Summary collapse

Instance Method Details

#even?Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/active_support/core_ext/integer/even_odd.rb', line 19

def even?
  multiple_of? 2
end

#multiple_of?(number) ⇒ Boolean

Returns:

  • (Boolean)

15
16
17
# File 'lib/active_support/core_ext/integer/even_odd.rb', line 15

def multiple_of?(number)
  self % number == 0
end

#odd?Boolean

Returns:

  • (Boolean)

23
24
25
# File 'lib/active_support/core_ext/integer/even_odd.rb', line 23

def odd?
  !even?
end