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

Deprecated.

Avoid using this any more. It's inaccurate and not very useful.

Performs to_s.to_i on the number. Note that for floats, the decimal counts as a digit within the string.

Parameters:

  • selection (Range)

    the selection/range to get from the number (you can use anything that works with the [] syntax)

Returns:

  • (Integer)

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


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
# File 'lib/magician/numeric.rb', line 11

def divisible? n
  not n.zero? and modulo(n).zero?
end

#to_degreesNumeric

Converts the number from radians to degrees and returns the result.

Returns:

  • (Numeric)

    the number in degrees


39
40
41
# File 'lib/magician/numeric.rb', line 39

def to_degrees
  self * 180 / Math::PI
end

#to_radiansNumeric

Converts the number from degrees to radians and returns the result.

Returns:

  • (Numeric)

    the number in radians


32
33
34
# File 'lib/magician/numeric.rb', line 32

def to_radians
  self * Math::PI / 180
end