Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/temperature/string.rb
Instance Method Summary collapse
-
#to_degrees ⇒ Object
Parse a string that looks like a temperature into a temperature of the right units.
Instance Method Details
#to_degrees ⇒ Object
Parse a string that looks like a temperature into a temperature of the right units. It handles strings of the format
35.1C
72K
-40F
Decimals are support, as are negative numbers. The units must be C, K, or F, and not have a space between the numer and the units
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/temperature/string.rb', line 13 def to_degrees if self =~ /^(-?)(\d+)\.(\d+)(F|C|K|R)$/ tmp = "#{$1}#{$2}.#{$3}".to_f tmp.units = $4 return tmp elsif self =~ /^(-?)(\d+)(F|C|K|R)$/ tmp = "#{$1}#{$2}".to_f tmp.units = $3 return tmp end end |