I wanted to get image url "http://www.test.com/image.jpg" out from the string:
"<img align="right" alt="Title " src="http://www.test.com/image.jpg" width="120" /><"
Here is the code that I have:
module MyHelper
def getMymage(allDesc)
allDesc = "<img align="right" alt="Title " src="http://www.test.com/image.jpg" width="120" /><"
allDesc = allDesc.scan(src="(\S+)")
end
end
I got the following error:
syntax error, unexpected tAMPER
allDesc = allDesc.scan(src="(\S+)")
syntax error, unexpected $undefined
allDesc = allDesc.scan(src="(\S+)")
How to fix it?
Can't comment on sunkencity's answer, but regex that solves the dash problem is:
/src=\"([a-z0-9_.\-:\/]+)"/i
The regexp is missing a start "/" and some extra stuff
allDesc.scan(/src=\"([a-z0-9_.\-:\/]+)"/i)
but you get an array as a response:
=> [["http://www.test.com/image.jpg"]]
I'd suggest using the matching operator and then use the first match variable:
allDesc =~ /(http:\/\/[a-z0-9_.-i\/]+)/ && $1
Related
I have written a rails application but when running rails server I am getting following error
"syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '(' ...? ? render("result") : Your url have a problem );#output_buf... ... ^"
</div>
</div>
<%= #links.present? ? render("result") : Your url have a problem %>
</div>
You need to enclose strings between " " or ' ' otherwise they will be treated as variables (or classes); Your url have a problem has no "", so that's causing the problem.
Try changing this line:
<%= #links.present? ? render("result") : Your url have a problem %>
to:
<%= #links.present? ? render("result") : "Your url have a problem" %>
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 /^## /.
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}
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?)