问题描述
当我尝试使用以下 RakeFile 运行 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.
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
我有一个简单的 Lexicon 类:
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
我想看看扫描方法是否有效.我正在学习 Ruby,所以我是该语言的新手,这是我的第二个 RakeFile 测试.我做错了什么?
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?
推荐答案
def self.scan
使方法成为一个类,而不是实例方法(在类的实例上调用).或者简单地使用 Lexicon.new(args).scan
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
这篇关于Rake NoMethodError:Lexicon:Class 的未定义方法“扫描"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!