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}
Related
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 /^## /.
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.
I'm playing around with rspec and running into an issue trying to check with Dir.glob. It looks like I'm having an error with closing out my Dir.stub statement, but all of my parentheses match?
syntax error, unexpected tCONSTANT, expecting ')' (SyntaxError)
Facter::Util::Resolution.stubs(:exe...
The code is
Dir.stubs(:glob).with("/opt/test/cli-*/checker").and_return(double("glob", :'exists?' => true)
Facter::Util::Resolution.stubs(:exec).with('checker -version').returns("Version: 1.5")
closing paren missing ) .
.and_return(double("glob", :'exists?' => true) <~~ here
Write as
.and_return(double("glob", :'exists?' => true))
In my profile.html.erb file I get a syntax error anytime I try to assign a class or id to erb. Below is an example:
<p>"<%= current_user.current_program.name, :id => 'progress' %>" Progress</p>
This gives me the following error:
SyntaxError in Users#profile
Showing /.../app/views/users/profile.html.erb where line #13 raised:
/Users/.../app/views/users/profile.html.erb:13: syntax error, unexpected tASSOC, expecting tCOLON2 or '[' or '.'
...er.current_program.name, :id => 'progress' );#output_buffer....
... ^
I can't figure out what the syntax error is. I'm totally stumped.
We can reproduce and simplify your problem in a standalone Ruby like so:
require 'erb'
ERB.new("<p><%= name, :a => 'b' %></p>").run
Producing the error:
SyntaxError: (erb):1: syntax error, unexpected tASSOC, expecting tCOLON2 or '[' or '.'
..."; _erbout.concat(( name, :a => 'b' ).to_s); _erbout.concat ...
... ^
from /Users/phrogz/.../ruby/1.9.1/erb.rb:838:in `eval'
from /Users/phrogz/.../ruby/1.9.1/erb.rb:838:in `result'
from /Users/phrogz/.../ruby/1.9.1/erb.rb:820:in `run'
from (irb):2
from /Users/phrogz/.../bin/irb:16:in `<main>'
Even more simply, taking ERB out of the mix:
a, :b=>'c'
#=> SyntaxError: (irb):3: syntax error, unexpected tASSOC, expecting tCOLON2 or '[' or '.'
What you have just isn't valid Ruby code. What were you trying to do there? Pass the :id => 'progress' hash as a parameter to the .name method? If so, then drop the comma, and (optionally) include parentheses for clarity:
<p>"<%= current_user.current_program.name( :id=>'progress' ) %>" Progress</p>
And if you're using Ruby 1.9+, you can use the simpler Hash-with-symbol-keys syntax:
<p>"<%= current_user.current_program.name( id:'progress' ) %>" Progress</p>
However, it seems unlikely to me that the name method takes such a hash, so I ask again: what are you really trying to accomplish? What does the name method return, and what HTML output do you want?
Taking a guess, maybe you wanted the text returned by .name to be wrapped in <span id="progress">? If so, you must do so like:
<p>"<span id="progress"><%= current_user.current_program.name%></span>" Progress</p>
Or perhaps using content_tag:
<p><%= content_tag("span", current_user.current_program.name, id:'progress') %> Progress</p>
In Haml this would be:
%p
%span#progress= current_user.current_program.name
Progress
maybe if you remove the comma it will work (is current_user.current_program.name a method that takes a hash as a parameter?)
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