Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 创建一个空扫描函数_Ruby_File - Fatal编程技术网

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
以下是步骤:

  • 在顶部写下require。让它发挥作用
  • 创建第一个测试用例测试方向的空版本。确保它运行正常
  • 写下测试用例的第一行。让它失败
  • 转到
    lexicon.rb
    文件,创建一个空扫描函数
  • 运行测试,确保扫描至少正在运行,即使扫描失败
  • 填写psuedo代码注释,了解扫描应如何工作以使测试通过
  • 编写与注释匹配的代码,直到测试通过
  • 返回测试方向并写出其余的行
  • 返回到lexicon.rb中的scan并对其进行操作,以使新的测试代码通过
  • 一旦你完成了,你就有了第一次通过的测试,然后你就进入下一个测试
  • 无论如何,我如何创建一个空扫描函数?任何答案都可以。

    def扫描 结束

    将上述函数添加到文件中。

    添加代码

    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
    类中,你的代码仍然可以工作,因为这个(实例)方法永远不会被调用。是的,这是最重要的事情,尤其是当你为一个问题挣扎了一段时间的时候。你现在可以休息一下了,去吧,杀了,吃了!