Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/magician/numeric.rb

Overview

Magician’s extensions to the Numeric class (affects Integers and Floats).

Instance Method Summary collapse

Instance Method Details

#digits(selection) ⇒ Integer

Performs to_s.to_i on the number. Note that for floats, the decimal counts as a digit within the string. TODO: Let this intelligently convert back to an Integer or Float.

use anything that works with the [] syntax)

Integer

Parameters:

  • selection (Range)

    the selection/range to get from the number (you can

Returns:

  • (Integer)

    substring of the number (using []), converted to an



25
26
27
# File 'lib/magician/numeric.rb', line 25

def digits selection
  to_s[selection].to_i
end

#divisible?(n) ⇒ Boolean

Returns true if the number is evenly divisible by n. If n is equal to 0, it returns false, since numbers cannot be divided by 0 in real number arithmetic.

Parameters:

  • n (Numeric)

    the number that this number (self) should be divided by

Returns:

  • (Boolean)

    true if the number is evenly divisible by n



11
12
13
14
# File 'lib/magician/numeric.rb', line 11

def divisible? n
  return false if n.zero?
  (self % n).zero?
end