Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module Commentable
  def initialize
    @comments = []
  end
  def add_comment(comment)
    @comments << comment
  end
  alias :<< :add_comment

  def to_s
    output = "Course Comments:\n-------------\n"
    @comments.inject(output){|output, comment| output << "#{comment}\n"}
  end
end

class CourseEvaluation
  include Commentable
  attr_accessor :title,:teacher
  def initialize(title,teacher)
    super()
    @title,@teacher = title, teacher
  end
  def to_s
    output = "Course Title: #{@title}\n"
    output << "Teacher: #{@teacher}\n"
    output << super
  end
end

cis999 = CourseEvaluation.new("Beginning Geekness","Joe")
cis999.add_comment("it was fun")
cis999.add_comment("it was boring")
cis999 << "could have been easier"
puts cis999

class Product
  include Commentable
  attr_accessor :name,:price
  def initialize(name,price)
    super()
    @name,@price = name, price
  end
  def to_s
    output = "Product Name: #{@name}\n"
    output << "Price: $#{@price}\n"
    output << super
  end
end
puts
puts
music_player = Product.new("MP3 Player","299.99")
music_player.add_comment("works great!")
music_player.add_comment("wish it had more storage")
music_player << "does it support ogg"
puts music_player