cparfon
cparfon

Reputation: 177

ruby syntax error, unexpected ':', expecting keyword_then

I've recently updated ruby from 1.8.7.374 to 2.1.2p95 and I have a svn post-commit script which was working fine but now it fails.

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>"

Here's the errors I get:

    [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

Any help in solving this is much appreciated.

Upvotes: 0

Views: 1765

Answers (2)

Radix
Radix

Reputation: 2747

Error in line: when line =~ /^Modified: / || line =~ /^=+$/ || line =~ /^@@ /: 'gray', as error message mentioned. Just remove : after /^@@ /.

Upvotes: 0

DMKE
DMKE

Reputation: 4603

Since Ruby 1.9, case expressions don't allow a colon anymore (cf. this answer).

To fix it, change your code to

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

Upvotes: 2

Related Questions