简体   繁体   English

Rake NoMethodError:Lexicon:Class的未定义方法“扫描”

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

I am receiving a NoMethodError: undefined method 'scan' for Lexicon:Class when I try to run a rake test with the following RakeFile. 当我尝试使用以下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

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. 我正在学习Ruby,所以我是该语言的新手,这是我的第二次RakeFile测试。 What am I doing wrong? 我究竟做错了什么?

def self.scan in order to make the method a class one, not instance method(called on instances of a class). def self.scan为了使该方法成为一类,而不是实例方法(在类的实例上调用)。 Or simply use Lexicon.new(args).scan 或者简单地使用Lexicon.new(args).scan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义的方法...用于类(NoMethodError) - undefined method … for class (NoMethodError) 耙db:seed NoMethodError:未定义的方法`sample&#39; - Rake db:seed NoMethodError: undefined method `sample' 未定义的方法&#39;scan&#39;为nil:NilClass(NoMethodError) - Undefined method 'scan' for nil:NilClass (NoMethodError) “捆绑执行佣金测试”“ NoMethodError:ActiveRecord :: Base:Class的未定义方法&#39;web_console&#39;” - “bundle exec rake test” “NoMethodError: undefined method `web_console' for ActiveRecord::Base:Class” 红宝石类未定义方法(NoMethodError) - ruby class undefined method (NoMethodError) Ruby NoMethodError(未定义方法 ''...' for '...:Class' - Ruby NoMethodError (undefined method ''…' for '…:Class' 瑞克12.3仍然会产生NoMethodError:未定义的方法&#39;last_comment&#39; - Rake 12.3 still produces NoMethodError: undefined method 'last_comment' Rails 4-rake db:seed NoMethodError:nil:NilClass的未定义方法“名称” - Rails 4 - rake db:seed NoMethodError: undefined method `name' for nil:NilClass 耙子流产了! NoMethodError:未定义的方法`misc_info&#39; - rake aborted! NoMethodError: undefined method `misc_info' NoMethodError:未定义的方法`where&#39;for main:Object(rake task) - Rails 4 - NoMethodError: undefined method `where' for main:Object (rake task) - Rails 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM