Forum: Ruby refactor sentencelength solution

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alex C. (Guest)
on 2006-03-03 01:42
(Received via mailing list)
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?
E. Saynatkari (Guest)
on 2006-03-03 22:28
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
This topic is locked and can not be replied to.