简体   繁体   中英

`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
  • The above code is to calculate word frequency in a text file
  • Whenever I try to run this below command I get the following error int the intialize function in LineAnalyzer class

ruby module2_assignment.rb test.txt

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

Since I am a beginner in Ruby I can't figure out the error.

The issue is in this line

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

LineAnalyzer 's constructor requires two parameters, you're passing none

well, the issue of initialize: wrong number of arguments can be resolved in passed arguments into LineAnalyzer.new , but we still have broken script after those changes, so, as I see, this script in [WIP] status, and we need to make some more changes to make it works.

If you can share more details about the goal of analyzing here, it would be nice.

so, go to code, we need to remove call from this line:

calculate_word_frequency(@content,@line_number)

and fix initializer logic here:

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

btw you have a typo in this line:

@highest_count_across_lines = @@higest_wf_count.max

should be:

@highest_count_across_lines = @@highest_wf_count.max

so, we've resolved issue, but it still not good, coz output is nothing:

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

coz should be something like this:

 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)"

So, I think we have 2 options here:

  1. invest some more efforts to make it works
  2. try to find a similar solution

we can use this worked solution of module2_assignment , for example:

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

I hope it's help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM