Letter-scoring Scrabble class I wrote Ruby code (copied below) but was told that the code does not compile (it does on my computer) and that it does not answer the question:


  Create a class called Scrabble that implements a method called score that accepts one word as a parameter and returns the scrabble score associated with that word


The code compiles on my computer and provides the response that I believe was required.  Can anyone point out issues with the code quality? I realize I did not write any unit tests for this code.  

I could add more inline comments in the code (I added comments at the top of the class which I removed from the code I copied here).

class Scrabble

  attr_reader :word

  def initialize(word)
    @word = word
  end

  def score
    letters = word.upcase.split('')

    total = 0
    letters.each do |letter|
      total += letter_scores[letter]
    end

    total
  end

# scores for each letter
  def letter_scores
    {  "A"=&gt;1, "B"=&gt;3, "C"=&gt;3, "D"=&gt;2,
      "E"=&gt;1, "F"=&gt;4, "G"=&gt;2, "H"=&gt;4,
      "I"=&gt;1, "J"=&gt;8, "K"=&gt;5, "L"=&gt;1,
      "M"=&gt;3, "N"=&gt;1, "O"=&gt;1, "P"=&gt;3,
      "Q"=&gt;10, "R"=&gt;1, "S"=&gt;1, "T"=&gt;1,
      "U"=&gt;1, "V"=&gt;4, "W"=&gt;4, "X"=&gt;8,
      "Y"=&gt;4, "Z"=&gt;10
    }
  end
end


puts "Type in one word for scrabble scorer"
word = gets.chomp
puts "Scrabble score for " + word + " is " + Scrabble.new(word).score.to_s
Some comments:


score method: With functional - instead of imperative - style, you'll get write more compact code.
You may use the abstraction Hash#values_at.
letter_scores is constant. It can be a class constant, then.
String interpolation: Use "#{var} = #{value}" style.
[edit] As you say, word should be an argument of score, not the class initializer.


I'd write:

class Scrabble
  LETTER_SCORES = {...}

  def score(word)
    scores = LETTER_SCORES.values_at(*word.upcase.chars)
    scores.compact.reduce(0, :+)
  end
end

scrabble = Scrabble.new
puts("Type in one word for scrabble scorer:")
word = gets.strip
puts("Scrabble score for #{word} is #{scrabble.score(word)}")