Skip to content

Commit 3014f03

Browse files
author
cocoa
committed
chapter 24 examples
1 parent 752968c commit 3014f03

6 files changed

+125
-1
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ tree hierarchy
130130
│   ├── in_the_wild.rb
131131
│   └── magic_methods.rb
132132
├── chapter_24
133+
│   ├── alias_method.rb
134+
│   ├── document.rb
135+
│   ├── fixing_a_broken_class.rb
136+
│   ├── improving_existing_classes.rb
137+
│   └── wide_open_classes.rb
133138
├── chapter_25
134139
│   ├── class_methods_that_change_their_class.rb
135140
│   ├── document.rb
@@ -173,6 +178,6 @@ tree hierarchy
173178
│   └── document_spec.rb
174179
└── document-1.0.1.gem
175180
176-
35 directories, 128 files
181+
35 directories, 133 files
177182
178183
```

chapter_24/alias_method.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require './document.rb'
2+
3+
# fixing the String class to allow between string and document
4+
class String
5+
6+
# it copies the method '+' giving the fresh copy the name 'old_addition'
7+
alias_method :old_addition, :+
8+
9+
def +( other )
10+
if other.kind_of? Document
11+
new_content = self + other.content
12+
return Document.new(other.title, other.author, new_content)
13+
end
14+
old_addition( other )
15+
end
16+
end
17+
18+
# example
19+
doc = Document.new('Bio','Russ',' my resume.')
20+
new_doc = 'This is' + doc
21+
puts new_doc.inspect

chapter_24/document.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Document
2+
attr_accessor :title, :author, :content
3+
4+
def initialize(title, author, content)
5+
@title = title
6+
@author = author
7+
@content = content
8+
end
9+
10+
def words
11+
@content.split
12+
end
13+
14+
def word_count
15+
words.size
16+
end
17+
18+
def average_word_length
19+
len = words.inject(0.0) { |total, word| word.size+total }
20+
len/word_count
21+
end
22+
23+
end

chapter_24/fixing_a_broken_class.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require './document.rb'
2+
3+
# example using the ORIGINAL method
4+
empty_doc = Document.new( 'Empty', 'Russ', '')
5+
puts empty_doc.average_word_length
6+
7+
# fix the class before use
8+
class Document
9+
def average_word_length
10+
return 0.0 unless word_count != 0
11+
total = words.inject(0.0) { |result,word| word.size + result }
12+
total / word_count
13+
end
14+
end
15+
16+
# example using the FIXED method
17+
empty_doc = Document.new( 'Empty', 'Russ', '')
18+
puts empty_doc.average_word_length
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require './document.rb'
2+
3+
# fixing the String class to allow between string and document
4+
class String
5+
def +( other )
6+
if other.kind_of? Document
7+
new_content = self + other.content
8+
return Document.new(other.title, other.author, new_content)
9+
end
10+
result = self.dup
11+
result << other.to_str
12+
result
13+
end
14+
end
15+
16+
# example
17+
doc = Document.new('Bio','Russ',' my resume.')
18+
new_doc = 'This is' + doc
19+
puts new_doc.inspect

chapter_24/wide_open_classes.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# first definition of Document class
2+
class Document
3+
attr_accessor :title, :author, :content
4+
5+
def initialize(title, author, content)
6+
@title = title
7+
@author = author
8+
@content = content
9+
end
10+
end
11+
12+
13+
# a Document class instance
14+
cover_letter = Document.new( 'Letter', 'Russ', "Here's my resume")
15+
16+
17+
# reopen the class Document to insert a new method
18+
class Document
19+
def words
20+
@content.split
21+
end
22+
def word_count
23+
words.size
24+
end
25+
end
26+
27+
28+
# another method
29+
class Document
30+
def average_word_length
31+
len = words.inject(0.0) { |total, word| word.size+total }
32+
len/word_count
33+
end
34+
end
35+
36+
37+
# we can use now the new method
38+
puts "Average word length: #{cover_letter.average_word_length}"

0 commit comments

Comments
 (0)