I'm learning Ruby, and I'm having a problem while making a program.
I have a class "LineAnalyzer" that has 4 parameters (2 provided and 2 calculated). Both calculated params are: @high_wf_count (integer) and @high_wf_words (array). Then, I have this one:
class Solution < LineAnalyzer
attr_reader :analyzers,
:highest_count_across_lines,
:highest_count_words_across_lines
def initialize
@analyzers = []
end
def analyze_file
File.foreach('test.txt') do |line|
@analyzers << LineAnalyzer.new(line.chomp,@analyzers.length+1)
end
end
def calculate_line_with_highest_frequency
@highest_count_words_across_lines = []
@highest_count_across_lines = @analyzers.max_by do
|a| a.instance_variable_get(:@highest_wf_count)
end .instance_variable_get(:@highest_wf_count)
@highest_count_words_across_lines << @analyzers.each do
|a| a.instance_variable_get(:@highest_wf_count) == @highest_count_across_lines
end .instance_variable_get(:@highest_wf_words)
end
end
The problem is that I cannot append the array @highest_wf_count to @highest_count_words_across_lines in the way I've done (it returns nil). But, I've previously taken the integer @highest_wf_count in the same way perfectly.
Can anyone tell me where's the problem?
Thanks in advance!