Class: Numeric

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

Overview

Temperature works as a mixin on both the Numeric and String classes. The idea is to make manipulating temperatures as simple and natural as possible. If units are not specified, they are assume to be in degrees ‘F’. Yes, I realize this is US centric, but that’s where I live. In the future I’d like to auto detect that base on locale.

Example use:

freezing_in_C = 32.to_C

num = 0
num.units = "C"
freezing_in_F = num.to_F

Constant Summary collapse

CScale =

The scale factor between C and F

1.8
FOffset =

The offset between C and F

32
KOffset =

The offset between K and C

273.15
ROffset =

The offset between R and F

459.67

Instance Method Summary collapse

Instance Method Details

#c2fObject



87
88
89
90
91
# File 'lib/temperature/numeric.rb', line 87

def c2f
    num = self * CScale + FOffset
    num.units = "F"
    return num
end

#c2kObject



99
100
101
102
103
# File 'lib/temperature/numeric.rb', line 99

def c2k
    num = self + KOffset
    num.units = "K"
    return num
end

#c2rObject



131
132
133
134
135
# File 'lib/temperature/numeric.rb', line 131

def c2r
  num = self * CScale + (ROffset + FOffset)
  num.units = "R"
  return num
end

#dewpoint(rh) ⇒ Object

Compute the dewpoint for the temperature given a relative humidity. This is using the NOAA approximation for dewpoint calculation - en.wikipedia.org/wiki/Dew_point#Closer_approximation



147
148
149
150
151
152
153
154
155
156
# File 'lib/temperature/numeric.rb', line 147

def dewpoint(rh)
    units = self.units
    temp = self.to_C
    e_sub_s = 6.112 * Math.exp((17.76 * temp) / (temp + 243.5))
    e = rh.to_f * e_sub_s / 100
    dew = 243.5 * Math.log(e / 6.112) / (17.67 -  Math.log(e / 6.112))
    dew.units = "C"
    final = dew.send("to_#{units}")
    return final
end

#f2cObject



93
94
95
96
97
# File 'lib/temperature/numeric.rb', line 93

def f2c
    num = (self - FOffset) / CScale
    num.units = "C"
    return num
end

#f2kObject



111
112
113
# File 'lib/temperature/numeric.rb', line 111

def f2k
    self.f2c.c2k
end

#f2rObject



125
126
127
128
129
# File 'lib/temperature/numeric.rb', line 125

def f2r
  num = self + ROffset
  num.units = "R"
  return num
end

#is_C?Boolean

Is this a Celcius temperature, returns a boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/temperature/numeric.rb', line 31

def is_C?
    return self.units == "C"
end

#is_F?Boolean

Is this a Farenheit temperature, returns a boolean

Returns:

  • (Boolean)


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

def is_F?
    return self.units == "F"
end

#is_K?Boolean

Is this a Kelvin temperature, returns a boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/temperature/numeric.rb', line 36

def is_K?
    return self.units == "K"
end

#is_R?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/temperature/numeric.rb', line 40

def is_R?
    return self.units == "R"
end

#k2cObject



105
106
107
108
109
# File 'lib/temperature/numeric.rb', line 105

def k2c
    num = self - KOffset
    num.units = "C"
    return num
end

#k2fObject



115
116
117
# File 'lib/temperature/numeric.rb', line 115

def k2f
    self.k2c.c2f
end

#k2rObject



137
138
139
140
141
# File 'lib/temperature/numeric.rb', line 137

def k2r
  num = self * CScale
  num.units = "R"
  return num
end

#r2fObject



119
120
121
122
123
# File 'lib/temperature/numeric.rb', line 119

def r2f
  num = self - ROffset
  num.units = "R"
  return num
end

#to_CObject

Convert the temperature to Celcius. If it’s already in C, it returns itself.



57
58
59
60
61
62
63
64
# File 'lib/temperature/numeric.rb', line 57

def to_C
    case self.units
    when "F" then return self.f2c
    when "C" then return self
    when "K" then return self.k2c
    when "R" then return self.r2c
    end
end

#to_FObject

Convert the temperature to Farenheit. If it’s already in F, it returns itself.



46
47
48
49
50
51
52
53
# File 'lib/temperature/numeric.rb', line 46

def to_F
    case self.units
    when "F" then return self
    when "C" then return self.c2f
    when "K" then return self.k2f
    when "R" then return self.r2f
    end
end

#to_KObject

Convert the temperature to Kelvins. If it’s already in K, it returns itself.



68
69
70
71
72
73
74
75
# File 'lib/temperature/numeric.rb', line 68

def to_K
    case self.units
    when "F" then return self.f2k
    when "C" then return self.c2k
    when "K" then return self
    when "R" then return self.k2r
    end
end

#to_RObject

Convert the temperature to Rankines.



78
79
80
81
82
83
84
85
# File 'lib/temperature/numeric.rb', line 78

def to_R
  case self.units
  when "F" then return self.f2r
  when "C" then return self.c2r
  when "K" then return self.k2r
  when "R" then return self
  end
end

#unitsObject

The degree units the temperature is in. This defaults to “F” if not specified. Valid values are “C”, “F”, or “K” (R is not currently support… no one really uses that anyway).



161
162
163
164
165
166
167
168
# File 'lib/temperature/numeric.rb', line 161

def units
    if defined?(@units) && @units
        return @units
    else
        # this should auto detect the env, but for now, I live in the US
        return "F"
    end
end

#units=(units) ⇒ Object



170
171
172
173
174
175
# File 'lib/temperature/numeric.rb', line 170

def units=(units)
    if units =~ /^(C|F|K|R)/
        @units = units
    end
    return @units
end