Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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_Rspec - Fatal编程技术网

Ruby 猪拉丁语测试先行教学

Ruby 猪拉丁语测试先行教学,ruby,rspec,Ruby,Rspec,这是Ruby测试第一教学的Pig_拉丁语。这是我的密码。起初,它在运行,它停留在2辅音,但现在它不再运行了 def translate(a) if a.split(' ').size > 1 a.map {|x| pig(x)}.join(' ') else pig(a) end end def pig(word) vowels = %w(a o i e u) alphabet = ('a'..'z').to_a

这是Ruby测试第一教学的Pig_拉丁语。这是我的密码。起初,它在运行,它停留在2辅音,但现在它不再运行了

def translate(a)
    if a.split(' ').size > 1
        a.map {|x| pig(x)}.join(' ')
    else
        pig(a)
    end
end

def pig(word)
    vowels = %w(a o i e u)
    alphabet = ('a'..'z').to_a
    consonant = alphabet - vowels

    if vowels.include? word[0]
        word + 'ay'
    elsif consonant.include? word[0] && word[1]
        word[2..-1] + word[0..1] + 'ay'
   elsif consonant.include? word[0]
        word[1..-1] + word[0] + 'ay'
    else
        word
    end
end
我用rspec测试,这就是我得到的

(in /Users/thanhnguyen/Downloads/Test)
/Users/thanhnguyen/Downloads/Test/04_pig_latin/pig_latin_spec.rb:20:in `require': /Users/thanhnguyen/Downloads/Test/04_pig_latin/pig_latin.rb:55: syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)
    from /Users/thanhnguyen/Downloads/Test/04_pig_latin/pig_latin_spec.rb:20:in `<top (required)>'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
    from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
/Users/thanhnguyen/.rvm/rubies/ruby-2.1.1/bin/ruby -S rspec /Users/thanhnguyen/Downloads/Test/04_pig_latin/pig_latin_spec.rb -I/Users/thanhnguyen/Downloads/Test/04_pig_latin -I/Users/thanhnguyen/Downloads/Test/04_pig_latin/solution -f documentation -r ./rspec_config failed
请帮帮我!!!谢谢

问题解决了。那时我是个笨蛋。谢谢大家

pig_latin.rb:55:语法错误


错误回溯会告诉您出了什么问题:语法错误。仔细检查你的代码。每个if语句都需要相应的结尾。def也是如此。类也需要一个结尾,就像以do开头的代码块一样。

从外观上看,您还没有终止ifs。每个多行if/else都应该有一个匹配的端点


如果您不满意,请添加评论-投票或要求结束,给OP一个机会来改进此问题或下一个问题。请看一下更新。我仍然有同样的问题。在我运行示例代码时,它没有包含任何语法错误,但您只向我们展示了23行代码。堆栈跟踪引用第55行。给我们看完整的代码。你运行相同的rspec文件吗?我只有这些。仍然有语法错误
 if ...
     # happens if true
 else
     # happens if false
 end # <== don't forget this