Statistician III (#177)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Q. 2:

  1. Please do not post any solutions or spoiler discussion for this
    quiz until 48 hours have passed from the time on this message.

  2. Support Ruby Q. 2 by submitting ideas as often as you can! (A
    permanent, new website is in the works for Ruby Q. 2. Until then,
    please visit the temporary website at

    http://splatbang.com/rubyquiz/.

  3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby T. follow the discussion. Please reply to
the original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Statistician III

For this week’s quiz, we finish the Statistician series. If you don’t
recall, go back and look at quizzes #167 and #168, where we built a
module that collects line-based data with the help of some simple
metaprogramming. However, there are three more things that I’d like
implemented in the Statistician module, and that is your task this
week.

First, add support for numeric fields. In the current implementations,
everything is treated as a string, which forces the client to do the
necessary conversion. A field should remain the default type, if the
client does not specify a field as numeric.

Second, add support for client-specified default values. If the client
does not provide defaults, use zero and the empty string (for numeric
and string fields respectively).

Third, add a few basic statistics methods, so the client need not
implement typical behavior. For numeric fields, this would be min,
max, mean (i.e. average), and sum. For string fields, try mode
(i.e. most frequent) or freq (i.e. percent frequency a particular
string appears in a field). Go nuts and add other methods, if so
inclined.

I leave the design of these features up to you, but if you want an
example to code to, then extend one of the solutions from quiz #168 to
implement this client file:

require 'statistician'

class Defense < Statistician::Reportable
  rule "[The ]<name> wounds you[ with <attack>] for <amount>

point[s] of [ damage]."
rule “You are wounded for point[s] of damage.”

  field :amount, 0        # recognize 0 as numeric, sets field

type to numeric and default to zero
field :kind, “Common” # field type remains string, default set
to “Common”
end

# Other similarly extended Reportable classes go here... See quiz 

#168.

if __FILE__ == $0
  lotro = Statistician::Reporter.new(Defense, Offense, Defeat, 

Victory,
Healing, Regen, Comment,
Ignored)
lotro.parse(File.read(ARGV[0]))

  puts <<-EOT
Number of Offense records: #{Offense.size}    # Note: slightly

different from quiz #168
Total damage inflicted: #{Offense.sum.amount}
Average damage per Offense: #{Offense.mean.amount}

Most frequently hit creature: #{Offense.mode.name}
  EOT
end

Your solution may be based on a submission from quiz #168, though
completely rewritten solutions are also welcome (as long as they still
implement the requirements of those two earlier quizzes).

No takers, eh?

Okay, I’ll find a replacement #177 for Friday.