Class: Integer

Inherits:
Object show all
Defined in:
lib/shenanigans/integer/divisible_by.rb,
lib/shenanigans/integer/string_length.rb

Instance Method Summary collapse

Instance Method Details

#divisible_by(n) ⇒ Object

Checks whether the receiver is cleanly divisble by the argument.

Examples:

3.divisble_by(0) #=> false
3.divisble_by(2) #=> false
9.divisble_by(3) #=> true


7
8
9
10
11
# File 'lib/shenanigans/integer/divisible_by.rb', line 7

def divisible_by(n)
  return false if n.zero?

  self % n == 0
end

#string_lengthObject

Returns the length of the number’s string representation.

Examples:

0.string_length #=> 1
123.string_length #=> 3
-1.string_length #=> 2


7
8
9
10
11
12
# File 'lib/shenanigans/integer/string_length.rb', line 7

def string_length
  return 1 if zero?

  len = Math.log10(abs).floor.next
  positive? ? len : len.next
end