Reputation: 33
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
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.
Upvotes: 1
Views: 3036
Reputation: 990
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:
we can use this worked solution of module2_assignment
, for example:
I hope it's help
Upvotes: 0
Reputation: 30071
The issue is in this line
@analyzers=Array.new(@arr.length){LineAnalyzer.new}
LineAnalyzer
's constructor requires two parameters, you're passing none
Upvotes: 1