Module: MetricConversions
- Defined in:
- lib/metric_conversions/areas.rb,
lib/metric_conversions/lengths.rb,
lib/metric_conversions/version.rb,
lib/metric_conversions/volumes.rb,
lib/metric_conversions/temperatures.rb
Defined Under Namespace
Classes: Area
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
-
#convert(from, to) ⇒ Object
performs the actual unit length conversion given a value.
-
#is_metric?(unit) ⇒ Boolean
determines if a given unit is metric.
-
#round_2 ⇒ Object
rounds a number to 2 digits.
-
#to_cm(from) ⇒ Object
given a from value, convert it to centimeters.
-
#to_ft ⇒ Object
given inches, convert to feet.
-
#to_inches(from) ⇒ Object
given a from value, convert it to inches.
-
#to_km ⇒ Object
given centimeters, convert to kilometers.
-
#to_m ⇒ Object
given centimeters, convert to meters.
-
#to_mi ⇒ Object
given inches, convert to miles.
-
#to_mm ⇒ Object
given centimeters, convert to millimeters.
-
#to_yd ⇒ Object
given inches, convert to yards.
Instance Method Details
#convert(from, to) ⇒ Object
performs the actual unit length conversion given a value
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/metric_conversions/lengths.rb', line 14 def convert(from, to) # if we are converting from a metric value if(is_metric?(from)) # because from is metric, convert to centimeters cm = self.to_cm(from) # if we are converting to a metric value, convert the value if(is_metric?(to)) if(to == "mm") cm.to_mm.round_2 elsif(to == "cm") cm.round_2 elsif(to == "m") cm.to_m.round_2 elsif(to == "km") cm.to_km.round_2 end # the value we are converting to is not metric, so convert the value to inches else inches = cm.to_inches("cm") # if we are converting to English values, perform the conversion if(to == "in") inches.round_2 elsif(to == "ft") inches.to_ft.round_2 elsif(to == "yd") inches.to_yd.round_2 elsif(to == "mi") inches.to_mi.round_2 end end else # the value we are converting from is not metric inches = self.to_inches(from) # if the value we are converting to is metric, convert inches to centimeters and make the final conversion if(is_metric?(to)) cm = inches.to_cm("in") if(to == "mm") cm.to_mm.round_2 elsif(to == "cm") cm.round_2 elsif(to == "m") cm.to_m.round_2 elsif(to == "km") cm.to_km.round_2 end # the value we are converting to is in the English system, so perform the conversion else if(to == "in") inches.round_2 elsif(to == "ft") inches.to_ft.round_2 elsif(to == "yd") inches.to_yd.round_2 elsif(to == "mi") inches.to_mi.round_2 end end end end |
#is_metric?(unit) ⇒ Boolean
determines if a given unit is metric
9 10 11 |
# File 'lib/metric_conversions/lengths.rb', line 9 def is_metric?(unit) return (unit == "mm" || unit == "cm" || unit == "m" || unit == "km") end |
#round_2 ⇒ Object
rounds a number to 2 digits
4 5 6 |
# File 'lib/metric_conversions/lengths.rb', line 4 def round_2 (self * 100).round / 100.0 end |
#to_cm(from) ⇒ Object
given a from value, convert it to centimeters
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/metric_conversions/lengths.rb', line 114 def to_cm(from) if(from == "mm") self / 10.0 elsif(from == "cm") self * 1.0 elsif(from == "m") self * 100.0 elsif(from == "in") self * 2.54 else self * 100000.0 end end |
#to_ft ⇒ Object
given inches, convert to feet
134 135 136 |
# File 'lib/metric_conversions/lengths.rb', line 134 def to_ft self / 12.0 end |
#to_inches(from) ⇒ Object
given a from value, convert it to inches
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/metric_conversions/lengths.rb', line 94 def to_inches(from) if(from == "ft") self * 12.0 elsif(from == "yd") self * 36.0 elsif(from == "mi") self * 12.0 * 5280 elsif(from == "cm") self / 2.54 elsif(from == "in") self * 1.0 end end |
#to_km ⇒ Object
given centimeters, convert to kilometers
159 160 161 |
# File 'lib/metric_conversions/lengths.rb', line 159 def to_km self / 100000.0 end |
#to_m ⇒ Object
given centimeters, convert to meters
154 155 156 |
# File 'lib/metric_conversions/lengths.rb', line 154 def to_m self / 100.0 end |
#to_mi ⇒ Object
given inches, convert to miles
144 145 146 |
# File 'lib/metric_conversions/lengths.rb', line 144 def to_mi self.to_ft / 5280.0 end |
#to_mm ⇒ Object
given centimeters, convert to millimeters
149 150 151 |
# File 'lib/metric_conversions/lengths.rb', line 149 def to_mm self * 10.0 end |
#to_yd ⇒ Object
given inches, convert to yards
139 140 141 |
# File 'lib/metric_conversions/lengths.rb', line 139 def to_yd self.to_ft / 3.0 end |