簡體   English   中英

Rake NoMethodError:Lexicon:Class的未定義方法“掃描”

[英]Rake NoMethodError: undefined method 'scan' for Lexicon:Class

當我嘗試使用以下RakeFile運行rake測試時,我收到Lexicon:ClassNoMethodError:undefined method'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測試。 我究竟做錯了什么?

def self.scan為了使該方法成為一類,而不是實例方法(在類的實例上調用)。 或者簡單地使用Lexicon.new(args).scan

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM