如何解决nilClass错误的未定义方法'[]'?

时间:2015-09-25 17:48:55

标签: ruby-on-rails ruby class oop

每次运行代码时都会出现以下错误

undefined method '[]' for nilClass: NoMethodError

有两个类LineAnalyzerSolution类,问题似乎出现在LineAnalyzer calculate_word_frequency方法中,但我找不到错误。

代码如下所示:

class LineAnalyzer
  attr_accessor :highest_wf_count, :highest_wf_words, :linesCount, :content , :line_number

  @highest_wf_count = Hash.new(0)
  @highest_wf_words = Array.new(0)
  @content 
  @line_number

  def initialize(line, line_num)
    @content = line
    @line_number = line_num
    calculate_word_frequency()
  end

  def calculate_word_frequency()
    @content.split.each do |word|
      @highest_wf_count[word.downcase] += 1
    end
    max = 0
    @highest_wf_count.each_pair do |key, value| 
      if value > max 
        max = value
      end
    end
    word_frequency.each_pair do |key, value| 
      if value == max 
        @highest_wf_words << key
      end
    end 
  end
end

class Solution
  attr_accessor :highest_count_across_lines, :highest_count_words_across_lines, :line_analyzers

  @highest_count_across_lines = Hash.new()
  @highest_count_words_across_lines = Hash.new()
  @line_analyzers = Array.new()

  def analyze_file
    line_count = 0
    x = File.foreach('C:\x\test.txt')
    x.each do |line|
      line_count += 1
      @line_analyzers << LineAnalyzer.new(line, line_count)
    end
  end
end

solution = Solution.new
# Expect errors until you implement these methods
solution.analyze_file

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:0)

试试这个:

def calculate_word_frequency()
  @content.split.each do |word|
    next if word.blank?
    @highest_wf_count[word.downcase]+=1
  end
  max =0 
  @highest_wf_count.each_pair do |key, value| 
   if value > max 
     max = value
   end
  end
  word_frequency.each_pair do |key, value| 
   if value == max 
    @highest_wf_words << key
   end
  end 
end

答案 1 :(得分:0)

看起来您的问题是,当您第一次看到单词时尝试增加word_count时,您使用的哈希没有该键,因此无法增加它。第一次看到该键时,您应该将其设置为1,并且所有出现的值都应该增加它:

class LineAnalyzer
    attr_accessor :highest_wf_count, :highest_wf_words, :linesCount, :content , :line_number

    def initialize(line,line_num)
        @highest_wf_count = Hash.new(0)
        @highest_wf_words = Array.new(0)
        @content = line
        @line_number = line_num
        calculate_word_frequency()
    end

   def calculate_word_frequency()
       @content.split.each do |word|
           highest_wf_count.key?(word.downcase) ? @highest_wf_count[word.downcase]+=1 : @highest_wf_count[word.downcase]=1
       end
       max =0 
       @highest_wf_count.each_pair do |key, value| 
           if value > max 
              max = value
           end
        end
       highest_wf_count.each_pair do |key, value| 
          if value == max 
             @highest_wf_words << key
          end
       end 
    end
end

class Solution
    attr_accessor :highest_count_across_lines, :highest_count_words_across_lines, :line_analyzers
    @highest_count_across_lines = Hash.new()
    @highest_count_words_across_lines = Hash.new()

    def analyze_file
      line_count = 0
      x = File.foreach('C:\x\test.txt')
      x.each{ |line|
        line_count +=1
        line_analyzer << LineAnalyzer.new(line,line_count)
      }
      puts "line_analyzer: #{line_analyzer.inspect}"
   end

    def line_analyzer
        @line_analyzers ||= Array.new
    end
end

答案 2 :(得分:0)

Yolanda,嗨!我发现有关LineAnalyzer Class中的highest_wf_count的错误。 我的意思是&#34; highest_wf_count值是3&#34;。见下文。

class LineAnalyzer

    attr_accessor :highest_wf_count, :highest_wf_hash, :highest_wf_words, :linesCount, :content , :line_number

    def initialize(line,line_num) #<--- taking a line of text (content) and a line number
        @highest_wf_hash = Hash.new(0) 
        @highest_wf_count = highest_wf_count #<--- a number with maximum number of occurrences for a single word (calculated)
        @highest_wf_words = Array.new(0) #<--- an array of words with the maximum number of occurrences (calculated)
        @content = line #<--- the string analyzed (provided)
        @line_number = line_num #<--- the line number analyzed (provided)
        calculate_word_frequency()
    end

   def calculate_word_frequency()
       @content.split.each do |word|
           highest_wf_hash.key?(word.downcase) ? @highest_wf_hash[word.downcase]+=1 : @highest_wf_hash[word.downcase]=1
       end
       @highest_wf_count = 0
       @highest_wf_hash.each_pair do |key, value| 
           if value > @highest_wf_count 
              @highest_wf_count = value

           end
        end
       highest_wf_hash.each_pair do |key, value| 
          if value == @highest_wf_count 
             @highest_wf_words << key
             @highest_wf_count == value
          end
       end 
    end
end

但是仍然存在类解决方案

的问题
  • line_analyzer
  • calculate_line_with_highest_frequency
  • print_highest_word_frequency_across_lines

你知道怎么解决吗?%)