意外的方法调用

时间:2013-06-13 02:26:48

标签: ruby mongodb

我正在使用mongomapper在db中存储页面,我首先将它们编入索引。在索引方法中,我遍历每个单词,并检查它是否已经在单词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]为零。此外,这个方法实际上是从循环中调用的,当它只在构造函数中调用一次时:

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>'

0 个答案:

没有答案