ruby syntax error, unexpected ':', expecting keyword_then - ruby-on-rails

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.

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

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

Related

Using Faker Gem to Seed Data

I'm attempting to seed my database using the Faker gem, but am getting some error messages and can't see where I'm going wrong. My seeds.rb is:
10.times do
List.create! (
name: Faker::Company.buzzword,
shared_with: Faker::Internet.email,
user_id: 3
)
end
50.times do
Item.create! (
name: Faker::Company.bs,
list_id: Faker::Number.between(1, 10),
delegated_to: Faker::Internet.email,
user_id: 3
)
end
puts "Seed finished"
puts "#{List.count} lists created"
puts "#{Item.count} items created"
And the error messages are:
rake aborted!
SyntaxError: /Users/.../db/seeds.rb:3: syntax error, unexpected tLABEL
name: Faker::Company.buzzword,
^
/Users/.../db/seeds.rb:4: syntax error, unexpected tLABEL, expecting '='
shared_with: Faker::Internet.email,
^
/Users/.../db/seeds.rb:5: syntax error, unexpected tLABEL, expecting '='
user_id: 3
^
/Users/.../db/seeds.rb:11: syntax error, unexpected tLABEL
name: Faker::Company.bs,
^
/Users/.../db/seeds.rb:12: syntax error, unexpected tLABEL, expecting '='
list_id: Faker::Number.between(1, 10),
^
/Users/.../db/seeds.rb:12: syntax error, unexpected ',', expecting keyword_end
/Users/.../db/seeds.rb:14: syntax error, unexpected tLABEL, expecting '='
user_id: 3
^
/Users/.../db/seeds.rb:20: syntax error, unexpected end-of-input, expecting keyword_end
Can anyone clue me in to where I'm going wrong?
In Ruby you should never put whitespace between a method name and the opening parenthesis.
# Syntax error
List.create! (
# Correct
List.create!(
So to expand, your code should look like:
10.times do
List.create!(
name: Faker::Company.buzzword,
shared_with: Faker::Internet.email,
user_id: 3
)
end

syntax error, unexpected keyword_in, expecting '|'

What is wrong with that code?
%body
- In.all.each do |in|
= link_to in.name, categories_path(in: in.name)
= yield
My syntax errors:
/home/ubuntu/workspace/app/views/layouts/application.html.haml:10: syntax error, unexpected keyword_in, expecting '|' In.all.each do |in| ^
/home/ubuntu/workspace/app/views/layouts/application.html.haml:11: syntax error, unexpected keyword_in, expecting ')' ...e_false_true_false(( link_to in.name, categories_path (in: i... ... ^
/home/ubuntu/workspace/app/views/layouts/application.html.haml:11: syntax error, unexpected ( arg, expecting keyword_do or '{' or '(' ...k_to in.name, categories_path (in: in.name) ... ^
/home/ubuntu/workspace/app/views/layouts/application.html.haml:12: syntax error, unexpected ')', expecting tSTRING_DEND ));}\n", 0, false);end;_hamlout.push_text(" <end>\n #{ ^
/home/ubuntu/workspace/app/views/layouts/application.html.haml:12: syntax error, unexpected keyword_end ));}\n", 0, false);end;_hamlout.push_text(" <end>\n #{ ^
/home/ubuntu/workspace/app/views/layouts/application.html.haml:14: syntax error, unexpected '}', expecting tSTRING_DEND ));}\n</body>\n", -1, false);::Ha... ^
/home/ubuntu/workspace/app/views/layouts/application.html.haml:14: unterminated regexp meets end of file
/home/ubuntu/workspace/app/views/layouts/application.html.haml:14: syntax error, unexpected end-of-input, expecting tSTRING_DEND
Thanks!
in is a reserved keyword. Try using something else as the block variable.

Ruby IF statement with OR

if subject.downcase.include? product.name.downcase || body.downcase.include? product.name.downcase
puts "111"
end
Why is the above faulty in ruby? Is there a better way to write this?
This is the resulting error:
SyntaxError: (irb):7: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n'
... body.downcase.include? product.name.downcase
... ^
(irb):9: syntax error, unexpected keyword_end, expecting end-of-input
This translates to:
if subject.downcase.include? (product.name.downcase || body.downcase.include? product.name.downcase)
You need to add parentheses:
if subject.downcase.include?(product.name.downcase) || body.downcase.include? product.name.downcase
However probably this is more readable:
if [subject, body].any? {|element| element.downcase.include? product.name.downcase}

Lots of syntax errors in HAML view (using spree engine)

I have this simple HAML view, admin.html.haml:
!!!
%html{:xmlns => "http://www.w3.org/1999/xhtml"}
%head{"data-hook" => "admin_inside_head"}
(the view is quite large, I deleted most of it for clarity)
Going to http://localhost:3000/admin I get:
SyntaxError in Spree/admin/overview#index
Showing /Users/panayi/Dropbox/Sites/RAILS/engines/core/app/views/spree/layouts/admin.html.haml where line #2 raised:
/Users/panayi/Dropbox/Sites/RAILS/engines/core/app/views/spree/layouts/admin.html.haml:2: syntax error, unexpected '=', expecting ')'
...ut.attributes({}, nil, :xmlns => "http://www.w3.org/1999/...
... ^
/Users/panayi/Dropbox/Sites/RAILS/engines/core/app/views/spree/layouts/admin.html.haml:2: syntax error, unexpected ')', expecting '}'
..."http://www.w3.org/1999/xhtml")}>\n <head#{_hamlout.adjust_...
... ^
/Users/panayi/Dropbox/Sites/RAILS/engines/core/app/views/spree/layouts/admin.html.haml:3: syntax error, unexpected '=', expecting ')'
...tributes({}, nil, "data-hook" => "admin_inside_head")}></...
... ^
/Users/panayi/Dropbox/Sites/RAILS/engines/core/app/views/spree/layouts/admin.html.haml:3: syntax error, unexpected ')', expecting '}'
...ook" => "admin_inside_head")}></head>\n</html>\n", -1, fa...
... ^
/Users/panayi/Dropbox/Sites/RAILS/engines/core/app/views/spree/layouts/admin.html.haml:3: unknown regexp options - htl
/Users/panayi/Dropbox/Sites/RAILS/engines/core/app/views/spree/layouts/admin.html.haml:3: syntax error, unexpected $undefined
...nside_head")}></head>\n</html>\n", -1, false);::Haml::Util.h...
... ^
/Users/panayi/Dropbox/Sites/RAILS/engines/core/app/views/spree/layouts/admin.html.haml:3: unterminated string meets end of file
/Users/panayi/Dropbox/Sites/RAILS/engines/core/app/views/spree/layouts/admin.html.haml:3: syntax error, unexpected $end, expecting '}'
Extracted source (around line #2):
1: !!!
2: %html{:xmlns => "http://www.w3.org/1999/xhtml"}
3: %head{"data-hook" => "admin_inside_head"}
Trace of template inclusion: /Users/panayi/Dropbox/Sites/RAILS/engines/core/app/views/spree/layouts/admin.html.haml
The haml gem is loaded correctly (messing with the haml view indentation I get a Haml::SyntaxError), and it worked ok with erb views, before switching the spree views to haml.
Can anyone suggest what to check, to resolve the errors?
I spotted the problem:
Spree uses deface which is incompatible with HAML (see here).
The solution is to disable deface in the environment-specific config file (development.rb, production.rb, etc). Add this:
# Disable deface
config.deface.enabled = false

Syntax errors in html file

Not exactly sure what the errors are indicating. I am getting the following syntax errors:
compile error
app/views/students/student_fail.html.haml:33: syntax error, unexpected tIDENTIFIER, expecting ')'
... :student_fail_attribute params[:StudentFailState.true], pa...
^
app/views/students/student_fail.html.haml:33: syntax error, unexpected ')', expecting '='
...method], params[:text_method])
^
app/views/students/student_fail.html.haml:39: syntax error, unexpected kENSURE, expecting kEND
...\n", -2, false);_erbout;ensure;#haml_buffer = #haml_buffer.u...
^
app/views/students/student_fail.html.haml:40: syntax error, unexpected kENSURE, expecting kEND
app/views/students/student_fail.html.haml:42: syntax error, unexpected $end, expecting kEND
Here's the html:
:ruby
fields = if #step == 1
[ select_tag(:id, options_from_collection_for_select(Student.passed, :id, :selector_label, #student.id), :size => 10) ]
elsif #step == 2
form_for #student do |f| f.collection_select( :student_fail_attribute params[:StudentFailState.true], params[:value_method], params[:text_method])
end
#fields << render_sequence_nav(sequence_info, students_path)
fields << render(:partial => "resources_partials/sequence/nav", :locals => sequence_info.merge({:cancel_url => {:controller => :dashboard}}))
= render_form { fields }
Thanks for any response.
I think you are missing a comma between :student_fail_attribute and params[:StudentFailState.true].
You might want to think if params[:StudentFailState.true] is supposed to be there at all, unless it returns the collection you will most likely not get the expected results.

Resources