Ruby:如何"调用方法"在类的初始化方法中?

时间:2015-09-22 21:17:54

标签: ruby

我目前正在编写一个有两种方法的类。第一种方法是初始化方法,第二种方法是计算已经输入的文本行中最常用的单词的方法。我得到了关于类中哪些方法以注释形式完成的指示。在初始化方法中实现的一个这样的指令,我不明白究竟是什么问题是我:

  

#*调用calculate_word_frequency()方法。

我已尝试以下操作来执行说明,但我担心我根本不理解说明的背景:

def initialize(content, line_number)
  @content = content
  @line_number = line_number
  calculate_word_frequency()
end

如果代码使用rspec测试单元执行预期的职责,我提供了一种自我测试的方法,所以我知道这并没有产生预期的结果。

此外,我无法找到一个示例,其中类中的initialize方法用于初始化输入变量以外的任何其他方法。我认为这可能是一个有用的方向,可以用我的问题来理解如何在同一个类中调用另一个方法,在initialize方法中完成任何事情。

代码包含评论形式的说明:

#Implement a class called LineAnalyzer.
class LineAnalyzer
    #Implement the following read-only attributes in the LineAnalyzer class.
    #* highest_wf_count - a number with maximum number of occurrences for a single word (calculated)
    #* highest_wf_words - an array of words with the maximum number of occurrences (calculated)
    #* content,         - the string analyzed (provided)
    #* line_number      - the line number analyzed (provided)
  attr_reader :highest_wf_count, :highest_wf_words, :content, :line_number

  #Add the following methods in the LineAnalyzer class.
  #* initialize() - taking a line of text (content) and a line number
  #* calculate_word_frequency() - calculates result

    #Implement the initialize() method to:
    #* take in a line of text and line number
    #* initialize the content and line_number attributes
    #* call the calculate_word_frequency() method.
  def initialize(content, line_number)
    @content = content
    @line_number = line_number
  end

    #Implement the calculate_word_frequency() method to:
    #* calculate the maximum number of times a single word appears within
    #  provided content and store that in the highest_wf_count attribute.
    #* identify the words that were used the maximum number of times and
    #  store that in the highest_wf_words attribute.
  def calculate_word_frequency()
    words = content.split(" ")
    frequency = Hash.new
    highest_wf_count = Hash.new
    highest_wf_words = Hash.new
    words.each { |word| frequencies[word.downcase] += 1}
    frequencies = frequency.sort_by {|a, b| b }
    frequencies.reverse!
    highest_wf_count = frequencies.values[0].push
    highest_wf_words = frequencies.keys[0].push
  end
end

通过在initialize方法中调用外部方法或如何从方法中调用外部方法来解释有问题的指令意味着什么的任何帮助。

2 个答案:

答案 0 :(得分:0)

您正确地从初始化方法调用calculate_word_frequency()

如果您没有得到正确的输出,则说明会告诉您:

#Implement the calculate_word_frequency() method to:
#* calculate the maximum number of times a single word appears within
#  provided content and store that in the highest_wf_count attribute.
#* identify the words that were used the maximum number of times and
#  store that in the highest_wf_words attribute.
#* identify the words that were used the maximum number of times and
# store that in the highest_wf_words attribute.

从您的代码中

def calculate_word_frequency()
  words = content.split(" ")
  frequency = Hash.new
  highest_wf_count = Hash.new
  highest_wf_words = Hash.new
  words.each { |word| frequencies[word.downcase] += 1}
  frequencies = frequency.sort_by {|a, b| b }
  frequencies.reverse!
  highest_wf_count = frequencies.values[0].push
  highest_wf_words = frequencies.keys[0].push
end

您只需将结果存储在名为highest_wf_wordshighest_wf_count的本地变量中。我假设当测试要求your_instance.highest_wf_words时,它会尝试读取实例变量值并返回nil,因为它尚未初始化。

修复只是将值分配给实例变量@highest_wf_words@highest_wf_count而不是局部变量。

答案 1 :(得分:0)

仅供将来参考,以防其他人需要一点帮助。 我相信属性的顺序应该是以下顺序的访问者+调用calculate_word_frequency,如下所示:

attr_accessor :content,
              :line_number,
              :highest_wf_count,
              :highest_wf_words

def initialize(content, line_number)
  @content = content
  @line_number = line_number
  @highest_wf_count = 0
  @highest_wf_words = 0
  calculate_word_frequency(content)
end