Class: Thermostat

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(want_value = 0, range = 0, unit = 'celsius', current_value = 0) ⇒ Thermostat

Returns a new instance of Thermostat.



4
5
6
7
8
9
# File 'lib/thermostat.rb', line 4

def initialize(want_value = 0, range = 0, unit = 'celsius', current_value = 0)
  @current_value = current_value
  @want_value = want_value
  @range = range / 2
  @unit = unit
end

Instance Attribute Details

#aircoObject

Returns the value of attribute airco.



2
3
4
# File 'lib/thermostat.rb', line 2

def airco
  @airco
end

#current_valueObject

Returns the value of attribute current_value.



2
3
4
# File 'lib/thermostat.rb', line 2

def current_value
  @current_value
end

#heatingObject

Returns the value of attribute heating.



2
3
4
# File 'lib/thermostat.rb', line 2

def heating
  @heating
end

#rangeObject

Returns the value of attribute range.



2
3
4
# File 'lib/thermostat.rb', line 2

def range
  @range
end

#unitObject

Returns the value of attribute unit.



2
3
4
# File 'lib/thermostat.rb', line 2

def unit
  @unit
end

#want_valueObject

Returns the value of attribute want_value.



2
3
4
# File 'lib/thermostat.rb', line 2

def want_value
  @want_value
end

Instance Method Details

#all_offObject



47
48
49
50
51
# File 'lib/thermostat.rb', line 47

def all_off
  @heating = false
  @airco = false
  puts 'Perfect temperature'
end

#celsius_to_fahrenheitObject



15
16
17
# File 'lib/thermostat.rb', line 15

def celsius_to_fahrenheit
  @current_value = (@current_value * 1.8) + 32
end

#celsius_to_kelvinObject



11
12
13
# File 'lib/thermostat.rb', line 11

def celsius_to_kelvin
  @current_value += 273.15
end

#check_coolObject



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

def check_cool
  cool if @current_value > @want_value
end

#check_heatObject



107
108
109
# File 'lib/thermostat.rb', line 107

def check_heat
  heat if @current_value < @want_value
end

#check_if_unit_is_celsius_to_fahrenheit(sensor_unit) ⇒ Object



94
95
96
# File 'lib/thermostat.rb', line 94

def check_if_unit_is_celsius_to_fahrenheit(sensor_unit)
  celsius_to_fahrenheit if sensor_unit == 'celsius'
end

#check_if_unit_is_celsius_to_kelvin(sensor_unit) ⇒ Object



82
83
84
# File 'lib/thermostat.rb', line 82

def check_if_unit_is_celsius_to_kelvin(sensor_unit)
  celsius_to_kelvin if sensor_unit == 'celsius'
end

#check_if_unit_is_fahrenheit_to_kelvin(sensor_unit) ⇒ Object



86
87
88
# File 'lib/thermostat.rb', line 86

def check_if_unit_is_fahrenheit_to_kelvin(sensor_unit)
  fahrenheit_to_kelvin if sensor_unit == 'fahrenheit'
end

#check_if_unit_is_fahrnheit_to_celsius(sensor_unit) ⇒ Object



74
75
76
# File 'lib/thermostat.rb', line 74

def check_if_unit_is_fahrnheit_to_celsius(sensor_unit)
  fahrenheit_to_celsius if sensor_unit == 'fahrenheit'
end

#check_if_unit_is_kelvin_to_celsius(sensor_unit) ⇒ Object



78
79
80
# File 'lib/thermostat.rb', line 78

def check_if_unit_is_kelvin_to_celsius(sensor_unit)
  kelvin_to_celsius if sensor_unit == 'kelvin'
end

#check_if_unit_is_kelvin_to_fahrenheit(sensor_unit) ⇒ Object



90
91
92
# File 'lib/thermostat.rb', line 90

def check_if_unit_is_kelvin_to_fahrenheit(sensor_unit)
  kelvin_to_fahrenheit if sensor_unit == 'kelvin'
end

#check_if_unit_to_celsius(sensor_unit) ⇒ Object



59
60
61
62
# File 'lib/thermostat.rb', line 59

def check_if_unit_to_celsius(sensor_unit)
  check_if_unit_is_fahrnheit_to_celsius(sensor_unit) if @unit == 'celsius'
  check_if_unit_is_kelvin_to_celsius(sensor_unit) if @unit == 'celsius'
end

#check_if_unit_to_fahrenheit(sensor_unit) ⇒ Object



69
70
71
72
# File 'lib/thermostat.rb', line 69

def check_if_unit_to_fahrenheit(sensor_unit)
  check_if_unit_is_celsius_to_fahrenheit(sensor_unit) if @unit == 'fahrenheit'
  check_if_unit_is_kelvin_to_fahrenheit(sensor_unit) if @unit == 'fahrenheit'
end

#check_if_unit_to_kelvin(sensor_unit) ⇒ Object



64
65
66
67
# File 'lib/thermostat.rb', line 64

def check_if_unit_to_kelvin(sensor_unit)
  check_if_unit_is_fahrenheit_to_kelvin(sensor_unit) if @unit == 'kelvin'
  check_if_unit_is_celsius_to_kelvin(sensor_unit) if @unit == 'kelvin'
end

#check_unit(sensor_unit) ⇒ Object



53
54
55
56
57
# File 'lib/thermostat.rb', line 53

def check_unit(sensor_unit)
  check_if_unit_to_celsius(sensor_unit)
  check_if_unit_to_fahrenheit(sensor_unit)
  check_if_unit_to_kelvin(sensor_unit)
end

#coolObject



41
42
43
44
45
# File 'lib/thermostat.rb', line 41

def cool
  @heating = false
  @airco = true
  puts 'Airco will be turned on.'
end

#fahrenheit_to_celsiusObject



27
28
29
# File 'lib/thermostat.rb', line 27

def fahrenheit_to_celsius
  @current_value = (@current_value - 32) / 1.8
end

#fahrenheit_to_kelvinObject



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

def fahrenheit_to_kelvin
  @current_value = ((@current_value - 32) / 1.8) + 273.15
end

#heatObject



35
36
37
38
39
# File 'lib/thermostat.rb', line 35

def heat
  @heating = true
  @airco = false
  puts 'Heat will be turned on.'
end

#it_in_rangeObject



98
99
100
101
102
103
104
105
# File 'lib/thermostat.rb', line 98

def it_in_range
  if (@current_value - @want_value).abs > @range
    check_heat
    check_cool
  else
    all_off
  end
end

#kelvin_to_celsiusObject



19
20
21
# File 'lib/thermostat.rb', line 19

def kelvin_to_celsius
  @current_value -= 273.15
end

#kelvin_to_fahrenheitObject



23
24
25
# File 'lib/thermostat.rb', line 23

def kelvin_to_fahrenheit
  @current_value = (@current_value - 273.15 * 1.8) + 32
end

#run(sensor_unit) ⇒ Object



115
116
117
118
# File 'lib/thermostat.rb', line 115

def run(sensor_unit)
  check_unit(sensor_unit)
  it_in_range
end