Class: Numeric
- Inherits:
-
Object
- Object
- Numeric
- 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
- #centimeters ⇒ Object (also: #cm, #centimeter)
- #cup ⇒ Object (also: #cups)
- #feet ⇒ Object (also: #ft, #foot)
-
#fluid_ounce ⇒ Object
(also: #fluid_ounces, #fl_oz)
VOLUME #####.
- #hours ⇒ Object (also: #hr, #hrs, #hour)
- #inches ⇒ Object (also: #in, #inch)
-
#kilograms ⇒ Object
(also: #kilogram, #kg, #kgs)
MASS #####.
- #kilometers ⇒ Object (also: #km, #kilometer)
-
#meters ⇒ Object
(also: #m, #meter)
DISTANCE/MEASUREMENTS/LENGTH #####.
- #miles ⇒ Object (also: #mi, #mile)
- #millimeters ⇒ Object (also: #mm, #millimeter)
- #minutes ⇒ Object (also: #min, #minute, #mins)
- #ounce ⇒ Object (also: #oz, #ounces)
- #pint ⇒ Object (also: #pints, #pt)
- #pounds ⇒ Object (also: #pound, #lb, #lbs)
- #quart ⇒ Object (also: #quarts, #qt)
-
#seconds ⇒ Object
(also: #second, #secs, #sec)
TIME ####.
- #stone ⇒ Object (also: #stones)
- #tablespoon ⇒ Object (also: #tablespoons, #tbsp)
- #teaspoon ⇒ Object (also: #teaspoons, #tsp)
-
#to(unit) ⇒ Object
Call using something like to(:pounds).
- #ton ⇒ Object (also: #tons)
- #yards ⇒ Object (also: #yd, #yard)
Instance Method Details
#centimeters ⇒ Object Also known as: cm, centimeter
64 65 66 |
# File 'lib/numeric/numeric_units.rb', line 64 def centimeters self * 0.01.meters end |
#cup ⇒ Object Also known as: cups
145 146 147 |
# File 'lib/numeric/numeric_units.rb', line 145 def cup self * 8.fl_oz end |
#feet ⇒ Object Also known as: ft, foot
70 71 72 |
# File 'lib/numeric/numeric_units.rb', line 70 def feet self * 0.3048.meters end |
#fluid_ounce ⇒ Object Also known as: fluid_ounces, fl_oz
VOLUME #####
127 128 129 |
# File 'lib/numeric/numeric_units.rb', line 127 def fluid_ounce self end |
#hours ⇒ Object Also known as: hr, hrs, hour
117 118 119 |
# File 'lib/numeric/numeric_units.rb', line 117 def hours self * 60.minutes end |
#inches ⇒ Object Also known as: in, inch
76 77 78 |
# File 'lib/numeric/numeric_units.rb', line 76 def inches self * 0.0254.meters end |
#kilograms ⇒ Object Also known as: kilogram, kg, kgs
MASS #####
19 20 21 |
# File 'lib/numeric/numeric_units.rb', line 19 def kilograms self end |
#kilometers ⇒ Object Also known as: km, kilometer
94 95 96 |
# File 'lib/numeric/numeric_units.rb', line 94 def kilometers self * 1000.meters end |
#meters ⇒ Object Also known as: m, meter
DISTANCE/MEASUREMENTS/LENGTH #####
52 53 54 |
# File 'lib/numeric/numeric_units.rb', line 52 def meters self end |
#miles ⇒ Object Also known as: mi, mile
88 89 90 |
# File 'lib/numeric/numeric_units.rb', line 88 def miles self * 1609.344.meters end |
#millimeters ⇒ Object Also known as: mm, millimeter
58 59 60 |
# File 'lib/numeric/numeric_units.rb', line 58 def millimeters self * 0.001.meters end |
#minutes ⇒ Object Also known as: min, minute, mins
110 111 112 |
# File 'lib/numeric/numeric_units.rb', line 110 def minutes self * 60.seconds end |
#ounce ⇒ Object Also known as: oz, ounces
33 34 35 |
# File 'lib/numeric/numeric_units.rb', line 33 def ounce self * 0.0283495231.kilograms end |
#pint ⇒ Object Also known as: pints, pt
150 151 152 |
# File 'lib/numeric/numeric_units.rb', line 150 def pint self * 16.fl_oz end |
#pounds ⇒ Object Also known as: pound, lb, lbs
26 27 28 |
# File 'lib/numeric/numeric_units.rb', line 26 def pounds self * 0.45359237.kilograms end |
#quart ⇒ Object Also known as: quarts, qt
156 157 158 |
# File 'lib/numeric/numeric_units.rb', line 156 def quart self * 16.fl_oz end |
#seconds ⇒ Object Also known as: second, secs, sec
TIME ####
103 104 105 |
# File 'lib/numeric/numeric_units.rb', line 103 def seconds self end |
#stone ⇒ Object Also known as: stones
39 40 41 |
# File 'lib/numeric/numeric_units.rb', line 39 def stone self * 6.35029318.kilograms end |
#tablespoon ⇒ Object Also known as: tablespoons, tbsp
139 140 141 |
# File 'lib/numeric/numeric_units.rb', line 139 def tablespoon self * 0.5.fl_oz end |
#teaspoon ⇒ Object 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 |
#ton ⇒ Object Also known as: tons
44 45 46 |
# File 'lib/numeric/numeric_units.rb', line 44 def ton self * 907.18474.kilograms end |
#yards ⇒ Object Also known as: yd, yard
82 83 84 |
# File 'lib/numeric/numeric_units.rb', line 82 def yards self * 3.feet end |