Skip to content

Instantly share code, notes, and snippets.

@smucode
Forked from jimweirich/presentation.rb
Created February 7, 2012 08:21
Show Gist options
  • Select an option

  • Save smucode/1758228 to your computer and use it in GitHub Desktop.

Select an option

Save smucode/1758228 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 3
class Pres
attr_accessor :what, :who
def initialize what, who, cat = "?"
@what = what
@who = who
@cat = cat
@scores = []
end
def add_score score
if (score < 1 || score > 5)
raise StandardError
end
@scores << score
end
def average_score
@scores.reduce(:+).to_f / @scores.size
end
end
require "./pres"
require "test/unit"
class PresTest < Test::Unit::TestCase
def test_average
p = Pres.new("How To Program in Ruby", "Ruby Hacker")
p.add_score(5)
p.add_score(4)
p.average_score
assert_equal 4.5, p.average_score
end
def test_illegal_value_less_than
p = Pres.new("How To Program in Ruby", "Ruby Hacker")
assert_raise StandardError do
p.add_score(-1)
end
end
def test_illegal_value_more_than
p = Pres.new("How To Program in Ruby", "Ruby Hacker")
assert_raise StandardError do
p.add_score(6)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment