0

我正在使用 mongomapper 将页面存储在数据库中,并且我首先对它们进行索引。在 index 方法中,我遍历每个单词,并检查它是否已经在单词 hashmap 中。如果没有,我将一个空数组添加到哈希中,然后将其位置推送到数组。

def index_words
  @words = self.body.split(" ")
  @words.each_with_index do |word,i|
    if self.words[word.stem].nil?
      self.words[word.stem] = []
    end
    puts "Called from #{caller[0]}"
    self.words[word.stem].push(i)
  end
end

当我运行它时,我得到一个未定义的方法错误,说它self.words[word.stem]是 nil。此外,这个方法实际上是从循环中调用的,而它只在构造函数中调用一次:

def initialize(*args)
  super
  index_words
end

错误信息是:

p = Page.new({author: 'Michael',url: 'michaelfine.me',title: 'Michael Fine',body: 'Body Text'})
called fromPage.rb:19:in `each'
NoMethodError: undefined method `push' for nil:NilClass
from Page.rb:24:in `block in index_words'
from Page.rb:19:in `each'
from Page.rb:19:in `each_with_index'
from Page.rb:19:in `index_words'
from Page.rb:14:in `initialize'
from (irb):103:in `new'
from (irb):103
from /Users/Michael/.rvm/rubies/ruby-1.9.3-p286/bin/irb:16:in `<main>'
4

0 回答 0