Rake NoMethodError: undefined method 'scan' for Lexicon:Class

随声附和 提交于 2020-01-17 06:12:21

问题


I am receiving a NoMethodError: undefined method 'scan' for Lexicon:Class when I try to run a rake test with the following RakeFile.

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

I have a simple Lexicon class:

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

and I want to see if the scan method is working. I'm learning Ruby, so I am new to the language, and this is my second RakeFile test. What am I doing wrong?


回答1:


def self.scan in order to make the method a class one, not instance method(called on instances of a class). Or simply use Lexicon.new(args).scan



来源:https://stackoverflow.com/questions/27729644/rake-nomethoderror-undefined-method-scan-for-lexiconclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!