这是Ruby的第一次教学的Pig_Latin。这是我的代码。起初,它运行并且它停留在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
帮帮我吧!!!感谢
问题解决了。那时我是个菜鸟。谢谢你们
答案 0 :(得分:2)
pig_latin.rb:55: syntax error
错误回溯告诉你错误:语法错误。仔细检查您的代码。每个if
语句都需要相应的end
。与def
相同。类也需要end
,代码块也以do
开头。
答案 1 :(得分:2)
从它的外观来看,你还没有终止你的if
。每条多行if/else
都应该匹配end
。
if ...
# happens if true
else
# happens if false
end # <== don't forget this