Skip to content

Commit

Permalink
TirePressureMonitoringSystem/Ruby: synchronised the code with the ori…
Browse files Browse the repository at this point in the history
…ginal C# version, minor syntax style improvements.
  • Loading branch information
Luca Minudel committed Sep 13, 2013
1 parent fb487a9 commit 614f5f4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
32 changes: 15 additions & 17 deletions TDDMicroExercises/Ruby/tire_pressure_monitoring_system/alarm.rb
@@ -1,24 +1,22 @@
require_relative "./sensor"
require_relative './sensor'

class Alarm
LOWPRESSURETHRESHOLD = 17
HIGHPRESSURETHRESHOLD = 21

def initialize
@sensor = Sensor.new
@alarm_on = false
@alarm_count = 0
end
attr_reader :alarm_on

def check
psi_pressure_value = @sensor.pop_next_pressure_psi_value
if psi_pressure_value < LOWPRESSURETHRESHOLD || HIGHPRESSURETHRESHOLD < psi_pressure_value
@alarm_on = true
@alarm_count += 1
end
end
def initialize
@sensor = Sensor.new
@alarm_on = false
end

def check
pressure = @sensor.pop_next_pressure_psi_value()

def alarm_on
return @alarm_on
@alarm_on = true if pressure < LOW_PRESSURE || HIGH_PRESSURE < pressure
end

private

LOW_PRESSURE = 17
HIGH_PRESSURE = 21
end
@@ -0,0 +1,9 @@
require 'test/unit'
require_relative './alarm'

class AlarmTest < Test::Unit::TestCase
def test_do_something
alarm = Alarm.new
alarm.check
end
end
23 changes: 12 additions & 11 deletions TDDMicroExercises/Ruby/tire_pressure_monitoring_system/sensor.rb
@@ -1,15 +1,16 @@
class Sensor
OFFSET = 16;
# The reading of the pressure value from the sensor is simulated in this implementation.
# Because the focus of the exercise is on the other class.

def pop_next_pressure_psi_value
pressure_telemetry_value = sample_pressure
OFFSET + pressure_telemetry_value;
end
OFFSET = 16
private_constant :OFFSET

def sample_pressure
#placeholder implementation that
#simulate a real sensor in a real tire
pressure_telemetry_value = 6 * rand(5) * rand(4)
pressure_telemetry_value
end
def pop_next_pressure_psi_value
OFFSET + read_sample_pressure()
end

def read_sample_pressure
#Simulate info read from a real sensor in a real tire
6 * rand * rand
end
end

0 comments on commit 614f5f4

Please sign in to comment.