ruby语法错误,意外':',期待keyword_then

时间:2016-03-13 13:18:36

标签: ruby-on-rails ruby svn

我最近将ruby从1.8.7.374更新到2.1.2p95并且我有一个svn post-commit脚本,它运行正常,但现在它失败了。

changes=`#{svnlook} diff #{repo} -r #{rev}`
body << "<pre>"
changes.each do |top_line|
top_line.split("\n").each do |line|
color = case
  when line =~ /^Modified: / || line =~ /^=+$/ || line =~ /^@@ /: 'gray'
  when line =~ /^-/ 'red:'
  when line =~ /^\+/ 'blue:'
  else "black"
  end
  body << %Q{<font style="color:#{color}">#{CGI.escapeHTML(line)}</font>    <br/>\n}
  end
  end
  body << "</pre>"

这是我得到的错误:

    [root@dev hooks]# ruby -c post-commit
post-commit:66: syntax error, unexpected ':', expecting keyword_then or     ',' or ';' or '\n'
...ne =~ /^=+$/ || line =~ /^@@ /: 'gray'
...                               ^
post-commit:67: syntax error, unexpected keyword_when, expecting    keyword_end
  when line =~ /^-/ 'red:'
      ^
post-commit:67: syntax error, unexpected tSTRING_BEG, expecting     keyword_end
      when line =~ /^-/ 'red:'
                     ^
post-commit:68: syntax error, unexpected keyword_when, expecting     keyword_end
      when line =~ /^\+/ 'blue:'
      ^
post-commit:68: syntax error, unexpected tSTRING_BEG, expecting     keyword_end
      when line =~ /^\+/ 'blue:'
                      ^
post-commit:69: syntax error, unexpected keyword_else, expecting     keyword_end
      else "black"
      ^
post-commit:65: warning: assigned but unused variable - color
post-commit:18: warning: assigned but unused variable - saddress
post-commit:20: warning: assigned but unused variable - sendmail
post-commit:73: syntax error, unexpected keyword_end, expecting end-of-    input

非常感谢任何解决此问题的帮助。

2 个答案:

答案 0 :(得分:2)

自Ruby 1.9起,case表达式不再允许冒号(参见this answer)。

要解决此问题,请将代码更改为

color = case
when line =~ /^Modified: / || line =~ /^=+$/ || line =~ /^@@ /
  'gray'
when line =~ /^-/
  'red:'
when line =~ /^\+/
  'blue:'
else
  "black"
end  

答案 1 :(得分:0)

行中出错:when line =~ /^Modified: / || line =~ /^=+$/ || line =~ /^@@ /: 'gray',提及错误消息。只需在:后删除/^@@ /