I am trying to learn Ruby and I am trying to call one instance method from another. My code is as follows:
class ScanTextInFile
attr_accessor :content , :line_number
def content
@content
end
def line_number
@line_number
end
def content= (text)
@content = text
end
def line_number= (line)
@line_number = line
end
def initialize (content , line_number)
self.content = content
self.line_number = line_number
#self.calculate_word_frequency
end
def calculate_word_frequency
words = self.content.split
words_hash = Hash.new
words.each do | word |
word.downcase!
if words_hash.has_key?
words_hash[key] += 1
else
words_hash[key] = 1
end
highest_wf_count = words_hash.values.max
words_hash.each { |key, value| highest_wf_words.push(key) }
end
end
end
I am trying to calculate the number of times a word appears in a given string. I am using the instance method calculate_word_frequency for that. When I comment out the call to calculate_word_frequency the code runs fine
$ irb -I
irb(main):001:0> require_relative 'test'
=> true
irb(main):002:0>
irb(main):003:0*
irb(main):004:0* a = ScanTextInFile.new("Bla bla foo foo bar baz foo bar bar", 1)
=> #<ScanTextInFile:0x007fc7a39b01c8 @content="Bla bla foo foo bar baz foo bar bar", @line_number=1>
However, when I call the instance method calculate_word_frequency in the initialize block, I get an error saying ArgumentError: wrong number of arguments (0 for 1)
$ irb -I
irb(main):001:0> require_relative 'test'
=> true
irb(main):002:0> a = ScanTextInFile.new("Bla bla foo foo bar baz foo bar bar", 1)
ArgumentError: wrong number of arguments (0 for 1)
I tried removing the self while calling the method but I still get error. I don't know what's going wrong when I make the call to the method. What is the correct way to do this?