Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save smucode/1759789 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 4
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
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
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
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