簡體   English   中英

`initialize': arguments 的錯誤編號(給定 0,預期為 2)(ArgumentError)

[英]`initialize': wrong number of arguments (given 0, expected 2) (ArgumentError)

class LineAnalyzer

 @@highest_wf_count=[]
 @@highest_wf_words=[]
 attr_accessor :highest_wf_count ,:highest_wf_words ,:content , :line_number


 def  initialize(line,num)
        @content=line
        @line_number=num
        calculate_word_frequency(@content,@line_number).call
 end

 def calculate_word_frequency(con,num)
        @content,@line_number=con,num
        @arr= @content.split()

       @arr.map do |txt|
               @count=0
               @i=0
               while @i<@content.length
                    @count+=1 if txt.eql?(@arr[@i])
                    @i+=1
               end
               @@highest_wf_count[@line_number]= @count
               @@highest_wf_words[@line_number]= txt
               @arr.delete(txt)
       end 
  end
end

class Solution < LineAnalyzer

 attr_accessor :analyzers, :highest_count_across_lines, :highest_count_words_across_lines

def initialize
       @analyzer=[]
       @highest_count_across_lines=0
       @highest_count_words_across_lines=[]
end

def analyze_file()
        @arr=IO.readlines(ARGV[0])
        @analyzers=Array.new(@arr.length){LineAnalyzer.new}
        @i=0
        @analyzer.each  do |obj|
                obj(@arr[@i],@i)
                @i+=1
        end
end

def calculate_line_with_highest_frequency()
     @highest_count_across_lines = @@higest_wf_count.max
     @i=0
     @@highest_wf_count.each do |count|
            @highest_count_words_across_lines.push @@highest_wf_words[@i]  if count==@highest_count_across_lines
            @i+=1
     end
 end
  • 上面的代碼是計算文本文件中的詞頻
  • 每當我嘗試運行以下命令時,我都會在 LineAnalyzer class 中的初始化 function 中收到以下錯誤

ruby module2_assignment.rb test.txt

錯誤:“初始化”:arguments 的編號錯誤(給定 0,預期為 2)(ArgumentError)

由於我是 Ruby 的初學者,我無法弄清楚錯誤。

問題出在這一行

@analyzers=Array.new(@arr.length){LineAnalyzer.new}

LineAnalyzer的構造函數需要兩個參數,你沒有傳遞

好吧, initialize: wrong number of arguments可以通過將 arguments 傳遞到LineAnalyzer.new來解決,但是在這些更改之后我們仍然有損壞的腳本,所以,正如我所見,這個腳本處於 [WIP] 狀態,我們需要進行更多更改以使其正常工作。

如果您可以在此處分享有關分析目標的更多詳細信息,那就太好了。

所以,go 代碼,我們需要從這一行刪除call

calculate_word_frequency(@content,@line_number)

並在此處修復初始化程序邏輯:

def initialize
    @analyzers=[]
    @highest_count_across_lines=0
    @highest_count_words_across_lines=[]
  end

  def analyze_file()
    @arr=IO.readlines(ARGV[0])
    @i=0
    @arr.each do |line|
      @analyzers << LineAnalyzer.new(line,@i)
      @i+=1
    end
  end

順便說一句,你在這一行有一個錯字:

@highest_count_across_lines = @@higest_wf_count.max

應該:

@highest_count_across_lines = @@highest_wf_count.max

所以,我們已經解決了問題,但仍然不好,因為 output 什么都不是:

ruby module2_assignment.rb test.txt
Test text
Test text
Test text
1
1
1

因為應該是這樣的:

 module2_assignment.rb test.txt
1
2
3
"The following words have the highest word frequency per line: "
"[\"test\", \"text\"] (appears in line 1)"
"[\"test\", \"text\"] (appears in line 2)"
"[\"test\", \"text\"] (appears in line 3)"

所以,我認為我們在這里有兩個選擇:

  1. 投入更多的努力使其發揮作用
  2. 嘗試找到類似的解決方案

我們可以使用module2_assignment這個有效的解決方案,例如:

https://github.com/zbristor/rubyWordFrequency/blob/2417324381378f6be76485f6271465cd641ec0ff/module2_assignment.rb

我希望它有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM