Refactor sentencelength solution


#1

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?


#2

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