Skip to content

Instantly share code, notes, and snippets.

@shostakovich
Created November 26, 2012 13:23
Show Gist options
  • Save shostakovich/4148171 to your computer and use it in GitHub Desktop.
Save shostakovich/4148171 to your computer and use it in GitHub Desktop.
Pressure Sensor
class Sensor
OFFSET_IN_MBAR = 1000
def current_pressure
OFFSET_IN_MBAR + random_pressure();
end
private
def random_pressure
Random.rand(1200)
end
end
class Alarm
LOWPRESSURE_THRESHOLD = 1700
HIGHPRESSURE_THRESHOLD = 2100
def initialize
@sensor = Sensor.new
@alarm_on = false
end
def check
pressure_value = @sensor.current_pressure
if (pressure_value < LOWPRESSURE_THRESHOLD || HIGHPRESSURE_THRESHOLD < pressure_value)
@alarm_on = true
end
end
def alarm_on?
@alarm_on
end
end
@shostakovich
Copy link
Author

Refactor + Test the code

  • Don't change the public interface of the classes
  • Try as many different approaches as you can find..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment