Skip to content

Instantly share code, notes, and snippets.

@cayblood
Forked from jimweirich/read_only_proxy.rb
Created February 8, 2012 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cayblood/1766976 to your computer and use it in GitHub Desktop.
Save cayblood/1766976 to your computer and use it in GitHub Desktop.
Vital Ruby Lab 5
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
$: << 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