Class: Winker::Temperature

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

Overview

TODO make temperture class for easy conversion between F and C this is not included in the project at this moment

Constant Summary collapse

DEFAULT_SCALE =
"f"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(temp, scale = DEFAULT_SCALE, display_scale = DEFAULT_SCALE) ⇒ Temperature

Returns a new instance of Temperature.



8
9
10
11
12
# File 'lib/winker/temperature.rb', line 8

def initialize(temp, scale = DEFAULT_SCALE, display_scale = DEFAULT_SCALE)
  @reading = temp
  @scale = scale
  @display_scale = display_scale
end

Instance Attribute Details

#display_scaleObject

Returns the value of attribute display_scale.



6
7
8
# File 'lib/winker/temperature.rb', line 6

def display_scale
  @display_scale
end

#readingObject

Returns the value of attribute reading.



6
7
8
# File 'lib/winker/temperature.rb', line 6

def reading
  @reading
end

#scaleObject

Returns the value of attribute scale.



6
7
8
# File 'lib/winker/temperature.rb', line 6

def scale
  @scale
end

Instance Method Details

#tempObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/winker/temperature.rb', line 26

def temp
  case
  when scale == display_scale
    return reading
  when scale == "f" && display_scale == "c"
    return (reading - 32) * 5.0 / 9
  when scale == "c" && display_scale == "f"
    return (reading * 9.0 / 5) + 32
  else
    raise "Doesn't support #{scale} temp scale"
  end
end

#to_fObject



22
23
24
# File 'lib/winker/temperature.rb', line 22

def to_f
  temp.to_f
end

#to_iObject



18
19
20
# File 'lib/winker/temperature.rb', line 18

def to_i
  temp.to_i
end

#to_sObject



14
15
16
# File 'lib/winker/temperature.rb', line 14

def to_s
  temp.to_s
end