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
56
|
class CourseEvaluation
attr_accessor :title,:teacher
def initialize(title,teacher)
@title,@teacher = title, teacher
@comments = []
end
def add_comment(comment)
@comments << comment
end
alias :<< :add_comment
def output_comments
output = "Course Comments:\n-------------\n"
@comments.inject(output){|output, comment| output << "#{comment}\n"}
end
def to_s
output = "Course Title: #{@title}\n"
output << "Teacher: #{@teacher}\n"
output << output_comments
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
attr_accessor :name,:price
def initialize(name,price)
@name,@price = name, price
@comments =[]
end
def add_comment(comment)
@comments << comment
end
alias :<< :add_comment
def output_comments
output = "Course Comments:\n-------------\n"
@comments.inject(output){|output, comment| output << "#{comment}\n"}
end
def to_s
output = "Product Name: #{@name}\n"
output << "Price: $#{@price}\n"
output << output_comments
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
|