-
-
Save smucode/1759789 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 4
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 Presentation | |
| attr_accessor :title, :author, :scores | |
| def initialize title, author | |
| @title = title | |
| @author = author | |
| @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("./presentation") | |
| describe Presentation do | |
| it "calculates the average" do | |
| p = Presentation.new("Some topic", "Some author") | |
| p.add_score(5) | |
| p.add_score(4) | |
| p.average_score.should eq(4.5) | |
| 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("./presentation") | |
| class Vote | |
| attr_accessor :presentations, :votes | |
| def initialize | |
| @votes = [] | |
| @presentations = {} | |
| read_presentations | |
| read_votes | |
| end | |
| def read_presentations | |
| open("presentations.txt").each do |line| | |
| num, title, author = line.split(/:/) | |
| @presentations[num.to_i] = Presentation.new title, author | |
| end | |
| end | |
| def read_votes | |
| open("votes.txt").each do |line| | |
| @votes << (num, vote = line.split.collect{ |e| e.to_i }) | |
| @presentations[num].add_score(vote) | |
| end | |
| end | |
| def get_above_4 | |
| @presentations.values.select { |v| v.average_score >= 4 } | |
| end | |
| def get_below_2 | |
| @presentations.values.select { |v| v.average_score <= 2 } | |
| 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("./vote") | |
| describe Vote do | |
| it "should read the presentations" do | |
| v = Vote.new | |
| v.presentations.size.should eq(24) | |
| end | |
| it "should read the votes" do | |
| v = Vote.new | |
| v.votes.size.should eq(250) | |
| end | |
| it "should return votes above 4" do | |
| v = Vote.new | |
| v.get_above_4.size.should eq(5) | |
| v.get_above_4.each { |p| p.average_score.should >= 4 } | |
| end | |
| it "should return votes below 2" do | |
| v = Vote.new | |
| v.get_below_2.size.should eq(11) | |
| v.get_below_2.each { |p| p.average_score.should <= 2 } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment