【问题标题】:How to solve the undefined method '[]' for nilClass error?如何解决 nilClass 错误的未定义方法“[]”?
【发布时间】:2015-09-25 17:48:54
【问题描述】:

我每次运行代码时都会收到以下错误

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

任何帮助将不胜感激。

【问题讨论】:

  • 你在哪里声明word_frequencyword_frequency.each_pair do |key, value|
  • word_frequency 应该是@highest_wf_count ... 代码错误
  • 我认为你是在混合类实例变量和实例变量,希望这个链接可以帮助你stackoverflow.com/questions/15773552/…。基本上,您将@highest_wf_words 声明为类实例变量,并且您试图将其用作实例变量,这就是您收到该错误的原因,因为在@highest_wf_count[word.downcase]+=1 行中,变量被声明为类实例变量。
  • 试试File.open('C:\x\test.txt')

标签: ruby-on-rails ruby class oop


【解决方案1】:

看起来您的问题是,当您尝试在第一次看到单词时增加 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

【讨论】:

  • 没有区别,当我尝试向 lineAnalyzers 数组添加值时出现错误 => @LineAnalyzers
  • @Yolanda,@ LineAnalyzers
  • @Hawkey001 你所做的代码修复是一个救命稻草……我现在明白什么意思了。谢谢大家
【解决方案2】:

试试这个:

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

【讨论】:

  • 我的问题是调用 File.foreach('C:\x\test.txt') do |line|
  • Prashant4020 noMethodError 尝试添加 LineAnalyzer 对象时
  • @Yolanda 你能输入begin @LineAnalyzers &lt;&lt; LineAnalyzer.new(line,line_count) rescue =&gt; e logger.error e.message logger.error e.backtrace.join("\n") end 以了解有关错误的更多信息
【解决方案3】:

约兰达,嗨!我在 LineAnalyzer 类中发现了有关highest_wf_count 的错误。 我的意思是“highest_wf_count 值为 3”。见下文。

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

你知道怎么解决吗?%)

【讨论】:

  • 请参考这个链接:有人帮我用几行代码解决了它,然后你可以尝试在课堂上弄清楚如何去做:pastie.org/10445014
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 2014-06-01
相关资源
最近更新 更多