Ruby语法错误:意外的keyword_end,期望输入结束

时间:2016-07-14 01:46:32

标签: ruby chef

我有一个似乎独立工作的块,但是当我把它放在代码示例中时,它会因语法错误而失败:

9 def get_version(name, user, pass, type, organization, art_module, repos, version)
10  puts case
11  when type.match(/snapshot$/i)
12    p version
13    when version.match(/latest$/i)
14         string_object = open("https://artifactory.xxx.io/artifactory/api/search/versions?g=#{organization}&v=*.*.?&a=#{art_module}&repos=#{repos}", :http_basic_authentication=>["#{user}", "#{pass}"])
15         json_file = JSON.parse(string_object.read)
16         version_array = Array.new
17         json_file["results"].each do |version|
18           version_array.push(version["version"].sub /-.*$/, '')
19         end
20         #p unique_versions=(version_array.uniq).max
21    else
22         p "here"
23         string_object = open("https://artifactory.xxx.io/artifactory/api/search/versions?g=#{organization}&v=*.*.?&a=#{art_module}&repos=#{repos}", :http_basic_authentication=>["#{user}", "#{pass}"])
24         json_file = JSON.parse(string_object.read)
25         version_array = Array.new
26         json_file["results"].each do |version|
27           version_array.push(version["version"].sub /-.*$/, '')
28         end
29         p unique_versions=(version_array.uniq).min
30    end
31  when type.match(/release$/i)
32  ...


SyntaxError
==> default: -----------
==> default: C:\vagrant-chef\a2c27477ebffe71b9594bbbb58557887\cookbooks\dj_productivity_any\providers\dj_artifactory_version.rb:26: syntax error, unexpected keyword_do_block, expecting keyword_end
==> default:          json_file["results"].each do |version|
==> default:                                      ^
==> default: C:\vagrant-chef\a2c27477ebffe71b9594bbbb58557887\cookbooks\dj_productivity_any\providers\dj_artifactory_version.rb:30: syntax error, unexpected keyword_end, expecting end-of-input

2 个答案:

答案 0 :(得分:1)

when仅在case内有效,看到您有elseend(关闭case),之后就有另一个when块(第30和31行)。

缩进你的代码并且更容易发现错误(可能你忘记在第13行之前开始一个新的case然后你的缩进是正确的)。

答案 1 :(得分:1)

我认为你正在使用案例,当你真正想要的只是简单的如果/其他

如果你正在测试一个的东西,你应该只 用例,例如:

case my_string
when /blue/
  do blue stuff
when /yellow/
  do yellow stuff
when /red/
  do red stuff
else
   do something else
end

你应该使用它来测试多个事情,例如你在这里做的事情:

case # nothing here to compare against the `when` cases
when type.match(/snapshot$/i) # here you are comparing type
when version.match(/latest$/i) # and now version
when type.match(/release$/i) # and now type again

同样......你有一个奇怪的嵌套,将无法正常工作。您在技术上进行编码:

case # open the case statement
when type.match(/snapshot$/i) # first match
when version.match(/latest$/i) # second match
else # this counts as the else clause of the first case
end # this has now closed the case statement completely
# and now there's another when... without a case... which is why yuour code is borking
when type.match(/release$/i) # and now type again

认为你真正要做的是在第一个案例中嵌套第二个案例陈述(如果我错了,请纠正我)> 如果是这样的话......你真的,真的,真的不需要案例陈述。只需使用if / else就像这样

if type.match(/snapshot$/i) # first match
  if version.match(/latest$/i) # second match
  else # this counts as the else clause of the second if-statement
  end # this has now closed the second if-statement
elsif type.match(/release$/i) # and now we test a second option against type
end # and this closes the first if-statement

如果你真的必须使用case语句,那么你需要做一些事情: a)实际指定你在案例陈述中的证词 - 紧接着case这个词之后 b)每次打开一组新的嵌套时重复关键字case ......例如:

case type
when /snapshot$/i # first case - first option (testing type)
  case version
  when /latest$/i # second case - first option (testing version)
  else # else clause of second case
  end # end of second case
when /release$/i # first case - second option (testing type)
end # end of first case

请注意,当只有一件事要测试时(例如第二个案例陈述),case-statement绝对是矫枉过正。