# Refactor sentencelength solution

**URL:** <https://www.ruby-forum.com/t/refactor-sentencelength-solution/76036>\
**Category:** Ruby\
**Created:** [March 3, 2006, 12:42am UTC](https://www.ruby-forum.com/t/refactor-sentencelength-solution/76036 "2006-03-03T00:42:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [March 3, 2006, 12:42am UTC](https://www.ruby-forum.com/t/refactor-sentencelength-solution/76036/1 "2006-03-03T00:42:17Z")

</div>

Hello,  
This is my solution to the Sentence length solution on the StartProblems  
page of the RubyGarden wiki.

I learned a few things from doing this simple exercise and I was  
just wondering if anyone would be willing to help me refactor the  
code as a further learning exercise.

I’m not looking for a new solution, I just want to make this existing  
solution better, and  
improve the OO of the code.

You can post replys to this email, or you can post replys  
on my blog, where I also have this posted.  
Either will be very appreciated!

class Evaluator  
def initialize(file)  
@reg = []  
@char = “”  
File.open(file, ‘r’) do |f|  
f.each do |line|  
@char \<\< line  
end  
end  
split\_and\_strip(@char)  
end

#split every sentence, then strip the newline characters.  
def split\_and\_strip(str)  
str.split(/[.?!]\s\*/).each do |x|  
@reg \<\< x.gsub("\n", " ")  
end  
end

def average?  
@char = @reg.inject(0) do |m,o|  
m + o.length  
end  
print "\nThe average sentence is “, (@char / @reg.length), "  
characters long.”  
end

def biggest?  
@char = @reg.inject(@reg[0].length) do |m,o|  
if m \>= o.length  
m  
else  
o.length  
end  
end  
print "\nThe biggest sentence is “, (@char), " characters long.”  
end

def smallest?  
@char = @reg.inject(@reg[0].length) do |m,o|  
if m \<= o.length  
m  
else  
o.length  
end  
end  
print "\nThe smallest sentence is “, (@char), " characters long.”  
end  
end

e = Evaluator.new(“words.txt”)  
e.smallest?  
e.average?  
e.biggest?

---

<div class="post-metadata">

**Author:** ![combas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@combas](https://www.ruby-forum.com/u/combas)\
**Post date:** [March 3, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/refactor-sentencelength-solution/76036/2 "2006-03-03T21:28:23Z")

</div>

Alex C. wrote:

> Hello,  
> This is my solution to the Sentence length solution on the StartProblems  
> page of the RubyGarden wiki.
> 
> I learned a few things from doing this simple exercise and I was  
> just wondering if anyone would be willing to help me refactor the  
> code as a further learning exercise.
> 
> I’m not looking for a new solution, I just want to make this existing  
> solution better, and  
> improve the OO of the code.
> 
> You can post replys to this email, or you can post replys  
> on my blog, where I also have this posted.  
> Either will be very appreciated!

This may be a bit buggy, but it takes advantage of the core.  
Regarding OO and so on there is not much to improve–except  
to say that you do not always need to use objects. Sometimes  
the simple procedural solution is just fine.

> class Evaluator  
> def initialize(file)  
> @reg = []  
> @char = “”  
> File.open(file, ‘r’) do |f|  
> f.each do |line|  
> @char \<\< line  
> end  
> end  
> split\_and\_strip(@char)  
> end

def initialize(file)  
@sentences = File.read(file).split /[.?!]\s\*/  
end

> #split every sentence, then strip the newline characters.  
> def split\_and\_strip(str)  
> str.split(/[.?!]\s\*/).each do |x|  
> @reg \<\< x.gsub("\n", " ")  
> end  
> end

# Split not needed, but let us write a cache

def stats()  
return @cached if @cached

```
# Compute using Enumerable
sorted = @sentences.sort_by {|s| s.length}
average = sorted.inject(0) {|i, s| i + s.length} / sorted.length

@cached = {:biggest => sorted.last,
           :smallest => sorted.first,
           :average => average}

```

end

> def average?  
> @char = @reg.inject(0) do |m,o|  
> m + o.length  
> end  
> print "\nThe average sentence is “, (@char / @reg.length), "  
> characters long.”  
> end

def average()  
@average ||= stats[:average]  
end

> def biggest?  
> @char = @reg.inject(@reg[0].length) do |m,o|  
> if m \>= o.length  
> m  
> else  
> o.length  
> end  
> end  
> print "\nThe biggest sentence is “, (@char), " characters long.”  
> end

def biggest()  
@biggest ||= stats[:biggest]  
end

> def smallest?  
> @char = @reg.inject(@reg[0].length) do |m,o|  
> if m \<= o.length  
> m  
> else  
> o.length  
> end  
> end  
> print "\nThe smallest sentence is “, (@char), " characters long.”  
> end  
> end

def smallest()  
@smallest ||= stats[:smallest]  
end

> e = Evaluator.new(“words.txt”)  
> e.smallest?  
> e.average?  
> e.biggest?

E
