-
-
Save smucode/1758228 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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