Skip to content

Instantly share code, notes, and snippets.

@mmower
Created November 25, 2009 08:31
Show Gist options
  • Save mmower/242573 to your computer and use it in GitHub Desktop.
Save mmower/242573 to your computer and use it in GitHub Desktop.
describe "Hospital Patient Chart" do
it "should have heart rate information" do
# heart rate information is numeric
# so it should be represented as a number
# it's implied that it's beats per minute
# (unless you're going to be storing it in
# some other format elsewhere)
HospitalPatientChart.new( 75 ).heart_rate.should == 75
end
end
class HospitalPatientChart
# We don't obviously need a setter so I've used attr_reader.
attr_reader :heart_rate
def initialize(heart_rate)
@heart_rate = heart_rate
end
end
# It's a patients chart so likely to be their heart rate
patient_chart = HospitalPatientChart.new( 75 )
puts patient_chart.heart_rate
#
# Very good, a big improvement.
#
describe "Hospital Patient Chart" do
it "should have heart rate information" do
patient_vital_signs = HospitalPatientChart.new( 80, 18, 98.6, 120 )
patient_vital_signs.pulse.should == 80
patient_vital_signs.respiration.should == 18
patient_vital_signs.body_temp.should == 98.6
patient_vital_signs.blood_pressure.should == 120
end
end
# Now consider the impact of the new stats you've added. You have a constructor with
# 4 values that are not easy to deduce on their own. Look at:
#
# HospitalPatientChart.new( 80, 18, 98.6, 120 )
#
# in isolation and see if you can figure out which is heart rate, which blood pressure,
# and so on.
#
# There are a number of ways to tackle such a situation but I might use a hash for
# initialization, e.g.
#
class HospitalPatientChart
attr_reader :pulse, :respiration, :body_temp, :blood_pressure
def initialize( vital_signs = {} )
check_vital_signs( vital_signs )
@pulse = vital_signs[:pulse]
@respiration = vital_signs[:respiration]
@body_temp = vital_signs[:body_temp].to_f
@blood_pressure = vital_signs[:blood_pressure]
end
def check_vital_signs( vital_signs )
missing_keys = vital_signs.keys.reject { |sign| vital_signs.has_key?( sign ) }
raise "Chart is missing vital signs #{missing_keys.join(',')}!" unless missing_keys.empty?
end
end
# Make sure you understand what I am doing here and the way that Ruby allows you to pass
# a hash as the last argument to a method call and the rules under which a hash does
# (or does not) require {} surrounding it.
patient_chart = HospitalPatientChart.new( :pulse => 80, :respiration => 18, :body_temp => 98.6, :blood_pressure => 120 )
puts "pulse: #{patient_chart.pulse}, respiration: #{patient_chart.respiration}, body temp: #{patient_chart.body_temp}, blood pressure: #{patient_chart.blood_pressure}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment