Rake NoMethodError:Lexicon:Class的未定义方法'scan'

时间:2015-01-01 09:32:33

标签: ruby rake nomethoderror

当我尝试使用以下RakeFile运行rake测试时,我收到 NoMethodError:针对Lexicon:Class 的未定义方法'scan'。

require './lib/ex48/lexicon.rb'
require 'test/unit'

class TestLexicon < Test::Unit::TestCase

    def test_directions
        assert_equal(Lexicon.scan('north'), [%w(direction north)])
    end

end

我有一个简单的词典类:

class Lexicon

  def initialize
    @direction = %w(north south east west down up left right back)
    @verbs = %w(go stop kill eat)
    @stop_words = %w(the in of from at it)
    @nouns = %w(door bear princess cabinet)
    @numbers = (0..9)
  end

  attr_reader :direction, :verbs, :stop_words, :nouns, :numbers

  def scan(input)
    words = input.split
    result = []

    words.each do |word|
      if @direction.include? word
        result.push ['direction', word]
        next
      end

      if @verbs.include? word
        result.push ['verb', word]
        next
      end

      if @stop_words.include? word
        result.push ['stop', word]
        next
      end

      if @nouns.include? word
        result.push ['noun', word]
        next
      end

      result.push ['number', word] if @numbers.include? word
    end

    result
  end

end

我希望看看扫描方法是否有效。我正在学习Ruby,所以我是这门语言的新手,这是我的第二次RakeFile测试。我做错了什么?

1 个答案:

答案 0 :(得分:3)

def self.scan以使方法成为第一类,而不是实例方法(在类的实例上调用)。或者只使用Lexicon.new(args).scan