Class: Numeric

Inherits:
Object show all
Defined in:
lib/nutella/core_ext/numeric/bytes.rb,
lib/nutella/core_ext/numeric/percent.rb,
lib/nutella/core_ext/numeric/sanity_check.rb

Constant Summary collapse

KILOBYTE =
1024
MEGABYTE =
KILOBYTE * 1024
GIGABYTE =
MEGABYTE * 1024
TERABYTE =
GIGABYTE * 1024
PETABYTE =
TERABYTE * 1024
EXABYTE =
PETABYTE * 1024

Instance Method Summary collapse

Instance Method Details

#bytesObject Also known as: byte



9
10
11
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 9

def bytes
  self
end

#exabytesObject Also known as: exabyte



39
40
41
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 39

def exabytes
  self * EXABYTE
end

#gigabytesObject Also known as: gigabyte



24
25
26
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 24

def gigabytes
  self * GIGABYTE
end

#kilobytesObject Also known as: kilobyte



14
15
16
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 14

def kilobytes
  self * KILOBYTE
end

#megabytesObject Also known as: megabyte



19
20
21
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 19

def megabytes
  self * MEGABYTE
end

#percentNumeric

Translates a percentage number to a decimal.

5.percent    # => 0.05
20.percent   # => 0.2
25.percent   # => 0.25
125.percent  # => 1.25
100.percent  # => 1
200.percent  # => 2

Returns:

  • (Numeric)

    the decimal for the percentage number



12
13
14
# File 'lib/nutella/core_ext/numeric/percent.rb', line 12

def percent
  self / 100.0
end

#petabytesObject Also known as: petabyte



34
35
36
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 34

def petabytes
  self * PETABYTE
end

#sanity_check_max(maximum) ⇒ Numeric

Checks that the number is at most a given maximum.

Examples:

Makes sure that a variable num is a maximum of 5

num = num.sanity_check_max 5

Parameters:

  • maximum (Numeric)

    the maximum that the number can be

Returns:

  • (Numeric)

    what the number must be in order to satisfy the maximum



20
21
22
# File 'lib/nutella/core_ext/numeric/sanity_check.rb', line 20

def sanity_check_max(maximum)
  [self, maximum].min
end

#sanity_check_min(minimum) ⇒ Numeric

Checks that the number is at least a given minimum.

Examples:

Makes sure that a variable num is a minimum of 5

num = num.sanity_check_min 5

Parameters:

  • minimum (Numeric)

    the minimum that the number can be

Returns:

  • (Numeric)

    what the number must be in order to satisfy the minimum



9
10
11
# File 'lib/nutella/core_ext/numeric/sanity_check.rb', line 9

def sanity_check_min(minimum)
  [self, minimum].max
end

#terabytesObject Also known as: terabyte



29
30
31
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 29

def terabytes
  self * TERABYTE
end