Class: Numeric

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

Overview

This file extends the Numeric class to provide simple utility for unit conversions. To properly use these extenstions, default units will/should be used:

> MASS = kilograms

> DISTANCE/MEASUREMENT = meters

> TIME = seconds

> VOLUME = fluid ounces

So, calling 3.pounds will return 1.36077711, which is the value converted to the base unit of kilograms.

When retrieving the values, you can call one of the to(unit) methods.

> 5.km.to(:miles) -> 3.10685596118667

whereas

> 5.km -> 5000 (meters)

Instance Method Summary collapse

Instance Method Details

#centimetersObject Also known as: cm, centimeter



64
65
66
# File 'lib/numeric/numeric_units.rb', line 64

def centimeters
  self * 0.01.meters
end

#cupObject Also known as: cups



145
146
147
# File 'lib/numeric/numeric_units.rb', line 145

def cup
  self * 8.fl_oz
end

#feetObject Also known as: ft, foot



70
71
72
# File 'lib/numeric/numeric_units.rb', line 70

def feet
  self * 0.3048.meters
end

#fluid_ounceObject Also known as: fluid_ounces, fl_oz

VOLUME #####



127
128
129
# File 'lib/numeric/numeric_units.rb', line 127

def fluid_ounce
  self
end

#hoursObject Also known as: hr, hrs, hour



117
118
119
# File 'lib/numeric/numeric_units.rb', line 117

def hours
  self * 60.minutes
end

#inchesObject Also known as: in, inch



76
77
78
# File 'lib/numeric/numeric_units.rb', line 76

def inches
  self * 0.0254.meters
end

#kilogramsObject Also known as: kilogram, kg, kgs

MASS #####



19
20
21
# File 'lib/numeric/numeric_units.rb', line 19

def kilograms
  self
end

#kilometersObject Also known as: km, kilometer



94
95
96
# File 'lib/numeric/numeric_units.rb', line 94

def kilometers
  self * 1000.meters
end

#metersObject Also known as: m, meter

DISTANCE/MEASUREMENTS/LENGTH #####



52
53
54
# File 'lib/numeric/numeric_units.rb', line 52

def meters
  self
end

#milesObject Also known as: mi, mile



88
89
90
# File 'lib/numeric/numeric_units.rb', line 88

def miles
  self * 1609.344.meters
end

#millimetersObject Also known as: mm, millimeter



58
59
60
# File 'lib/numeric/numeric_units.rb', line 58

def millimeters
  self * 0.001.meters
end

#minutesObject Also known as: min, minute, mins



110
111
112
# File 'lib/numeric/numeric_units.rb', line 110

def minutes
  self * 60.seconds
end

#ounceObject Also known as: oz, ounces



33
34
35
# File 'lib/numeric/numeric_units.rb', line 33

def ounce
  self * 0.0283495231.kilograms
end

#pintObject Also known as: pints, pt



150
151
152
# File 'lib/numeric/numeric_units.rb', line 150

def pint
  self * 16.fl_oz
end

#poundsObject Also known as: pound, lb, lbs



26
27
28
# File 'lib/numeric/numeric_units.rb', line 26

def pounds
  self * 0.45359237.kilograms
end

#quartObject Also known as: quarts, qt



156
157
158
# File 'lib/numeric/numeric_units.rb', line 156

def quart
  self * 16.fl_oz
end

#secondsObject Also known as: second, secs, sec

TIME ####



103
104
105
# File 'lib/numeric/numeric_units.rb', line 103

def seconds
  self
end

#stoneObject Also known as: stones



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

def stone
  self * 6.35029318.kilograms
end

#tablespoonObject Also known as: tablespoons, tbsp



139
140
141
# File 'lib/numeric/numeric_units.rb', line 139

def tablespoon
  self * 0.5.fl_oz
end

#teaspoonObject Also known as: teaspoons, tsp



133
134
135
# File 'lib/numeric/numeric_units.rb', line 133

def teaspoon
  self * (1.0/6.0)
end

#to(unit) ⇒ Object

Call using something like to(:pounds)



163
164
165
# File 'lib/numeric/numeric_units.rb', line 163

def to(unit)
  self / 1.send(unit)
end

#tonObject Also known as: tons



44
45
46
# File 'lib/numeric/numeric_units.rb', line 44

def ton
  self * 907.18474.kilograms
end

#yardsObject Also known as: yd, yard



82
83
84
# File 'lib/numeric/numeric_units.rb', line 82

def yards
  self * 3.feet
end