Ruby 创建一个空扫描函数
如何创建空扫描函数?这是我的Ruby 创建一个空扫描函数,ruby,file,Ruby,File,如何创建空扫描函数?这是我的lexicon.rb文件 require 'ex48/lexicon.rb' require "test/unit" class TestNAME < Test::Unit::TestCase def test_directions() assert_equal(Lexicon.scan("north"), [['direction', 'north']]) result = Lexicon.
lexicon.rb
文件
require 'ex48/lexicon.rb'
require "test/unit"
class TestNAME < Test::Unit::TestCase
def test_directions()
assert_equal(Lexicon.scan("north"), [['direction', 'north']])
result = Lexicon.scan("north south east")
assert_equal(result, [['direction', 'north'],
['direction', 'south'],
['direction', 'east']])
end
def test_verbs()
assert_equal(Lexicon.scan("go"), [['verb', 'go']])
result = Lexicon.scan("go kill eat")
assert_equal(result, [['verb', 'go'],
['verb', 'kill'],
['verb', 'eat']])
end
def test_stops()
assert_equal(Lexicon.scan("the"), [['stop', 'the']])
result = Lexicon.scan("the in of")
assert_equal(result, [['stop', 'the'],
['stop', 'in'],
['stop', 'of']])
end
def test_nouns()
assert_equal(Lexicon.scan("bear"), [['noun', 'bear']])
result = Lexicon.scan("bear princess")
assert_equal(result, [['noun', 'bear'],
['noun', 'princess']])
end
def test_numbers()
assert_equal(Lexicon.scan("1234"), [['number', 1234]])
result = Lexicon.scan("3 91234")
assert_equal(result, [['number', 3],
['number', 91234]])
end
def test_errors()
assert_equal(Lexicon.scan("ASDFADFASDF"), [['error', 'ASDFADFASDF']])
result = Lexicon.scan("bear IAS princess")
assert_equal(result, [['noun', 'bear'],
['error', 'IAS'],
['noun', 'princess']])
end
end
需要'ex48/lexicon.rb'
需要“测试/单元”
类TestNAME
以下是步骤:
lexicon.rb
文件,创建一个空扫描函数require 'ex48/lexicon.rb'
require "test/unit"
class TestNAME < Test::Unit::TestCase
def test_directions()
assert_equal(Lexicon.scan("north"), [['direction', 'north']])
result = Lexicon.scan("north south east")
assert_equal(result, [['direction', 'north'],
['direction', 'south'],
['direction', 'east']])
end
def test_verbs()
assert_equal(Lexicon.scan("go"), [['verb', 'go']])
result = Lexicon.scan("go kill eat")
assert_equal(result, [['verb', 'go'],
['verb', 'kill'],
['verb', 'eat']])
end
def test_stops()
assert_equal(Lexicon.scan("the"), [['stop', 'the']])
result = Lexicon.scan("the in of")
assert_equal(result, [['stop', 'the'],
['stop', 'in'],
['stop', 'of']])
end
def test_nouns()
assert_equal(Lexicon.scan("bear"), [['noun', 'bear']])
result = Lexicon.scan("bear princess")
assert_equal(result, [['noun', 'bear'],
['noun', 'princess']])
end
def test_numbers()
assert_equal(Lexicon.scan("1234"), [['number', 1234]])
result = Lexicon.scan("3 91234")
assert_equal(result, [['number', 3],
['number', 91234]])
end
def test_errors()
assert_equal(Lexicon.scan("ASDFADFASDF"), [['error', 'ASDFADFASDF']])
result = Lexicon.scan("bear IAS princess")
assert_equal(result, [['noun', 'bear'],
['error', 'IAS'],
['noun', 'princess']])
end
def scan()
end
end
def
scan()
end
加
def scan()结束
希望有帮助一个函数是
def
(your code)
end
def
scan ()
end
所以一个扫描函数是
def
(your code)
end
def
scan ()
end
我很确定这是一个
语法错误
,您需要一个换行符、分号或括号来告诉Ruby没有参数列表……另外scan
需要更改为self.scan
(因为它是一个类方法),并且该行需要包装在类词典中
/end
。除此之外,@JörgWMittag还指出了一个错误,这很好。你应该在扫描之后添加“()”。如果你要将def scan()end
添加到你的Lexicon
类中,你的代码仍然可以工作,因为这个(实例)方法永远不会被调用。是的,这是最重要的事情,尤其是当你为一个问题挣扎了一段时间的时候。你现在可以休息一下了,去吧,杀了,吃了!