title: Ruby: how to count the frequencies of the letters
body: <p>HELP: do a frequency method:  calculate the frequency each letter appears in “str” and assign the output to “letters”. The method should count lower and uppercase as the same letter. but I have the error about NoMethodError: undefined method 
  Thank you</p>
  
  <p>here is my code</p>
  
  <pre><code>class MyString
    attr_accessor :str
    attr_reader :letters
  
    def initialize
      @str = "Hello World!"
      @letters = Hash.new()
    end
    def frequency
      @letters = @str.split(" ").reduce(@letters) { |h, c| h[c] += 1; h}
    end
    def histogram
      #@letters.each{|key,value| puts "#{key} + ':' + value.to_s + '*' * #{value}" }
    end
  end
  </code></pre>
  
  <p>The error shows : </p>
  
  <pre><code> irb(main):009:0&gt; txt1.frequency
  NoMethodError: undefined method `+' for nil:NilClass
      from assignment.rb:11:in `block in frequency'
      from assignment.rb:11:in `each'
      from assignment.rb:11:in `reduce'
      from assignment.rb:11:in `frequency'
      from (irb):9
      from /usr/bin/irb:12:in `&lt;main&gt;'
  </code></pre>