vendredi 15 mai 2020

`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 amm a rookie in ruby I can't figure out the error. Please help me out. Thanks in advance!!!

Aucun commentaire:

Enregistrer un commentaire