-
-
Save cayblood/1766976 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 5
This file contains 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 Array | |
def sum | |
inject(0.0) { |result, el| result + el } | |
end | |
def mean | |
sum / size | |
end | |
end | |
class InvalidScoreError < RuntimeError | |
end | |
class Presentation | |
attr_accessor :presentation_id, :title, :presenter, :subject, :scores | |
def initialize(presentation_id, title, presenter, subject) | |
@presentation_id, @title, @presenter, @subject = [presentation_id, title, presenter, subject] | |
@scores = [] | |
end | |
def self.parse(line) | |
self.new(*line.split(':')) | |
end | |
def add_score(score) | |
raise InvalidScoreError if !score.is_a?(Integer) || score < 1 || score > 5 | |
@scores << score | |
end | |
def average_score | |
return @scores.mean unless @scores.empty? | |
0.0 | |
end | |
end |
This file contains 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
$: << File.dirname(__FILE__) | |
require 'presentation' | |
class ReadOnlyProxy | |
def initialize(target) | |
@target = target | |
end | |
def method_missing(sym, *args, &block) | |
if sym.to_s !~ /=$/ | |
@target.send(sym, *args, &block) | |
endx | |
end | |
end | |
p = Presentation.new("1", "Talking to Whales", "Phinehas Phisch", "oceanography") | |
ro = ReadOnlyProxy.new(p) | |
puts ro.title | |
ro.title = "HACKED" | |
puts ro.title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment