-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.rb
61 lines (50 loc) · 1.18 KB
/
document.rb
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
57
58
59
60
61
# Base document for testing
class Document
attr_accessor :title, :author, :content
def initialize title, author, content
@title = title
@author = author
@content = content
end
def words
@content.split
end
def word_count
words.size
end
def index_for( word ) i= 0
# words.each do |this_word|
# return i if word == this_word
# i += 1
# end
words.find_index { |this_word| word == this_word }
end
def average_word_length
total = words.inject(0.0){ |result, word| word.size + result}
total / word_count
end
def title=( new_title )
unless @read_only
@title = new_title
end
end
def author=( new_author )
unless @read_only
@author = new_author
end
end
def content=( new_content )
unless @read_only
@content = new_content
end
end
# Given a number, which needs to be an instance of Numeric,
# return true if the number of characters in the document
# exceeds the number.
def is_longer_than?( number_of_characters )
@content.length > number_of_characters
end
def clone
Document.new( title.clone, author.clone, content.clone )
end
end