I am trying to remove all double-quoted quotations in a string:
For example: "Mary said "Lookout"!?"
"Mary said "Lookout"!?" is coming from a html form with a textarea tag
<textarea id="receiver" name="receiver" class="form-control" maxlength= "1080" type="text"></textarea>
That is then put into a variable called Words. So:
Words = "Mary said "Lookout"!?"
Then I run
Words.gsub!(/[!?/A"|"/Z]/, "")
I want the output to read:
Mary said Lookout
Instead I am getting an error,
"Mary said "Lookout"!?".gsub!(/[!?/A"|"/Z]/, "")
SyntaxError: (irb):4: syntax error, unexpected tCONSTANT, expecting end-of-input "Mary said "Lookout"!?".gsub!(/[!?/A"|"/Z]/, "")
The error you are getting is because you have not escaped the speech marks. Ruby doesn't understand more than two speech marks in a row unless you tell it they're meant to be there. Try this:
"Mary said \"Lookout\"!?"
I believe there is still an issue with your gsub as well. Try that first and see if you can get further on your own.
Related
Working on exercise #26 of Learn Ruby The Hard Way -- correcting a ficticious programmer's bad code.
I've got most of it worked out, but can't even get to testing because I keep getting this syntax error:
syntax error, unexpected tIDENTIFIER, expecting ')'
...on this line:
sentence = "All good\tthings come to those who wait."
I thought that was always the way variables were declared? Since the error was listing parens, I tried those too -- around sentence (even though it made no sense), around the string (both with and without quotes), with the equals sign, without the equals sign...I'm not really sure what the issue is here.
Not always errors are on the same lines as interpreter says ;) So it would be better if you include some adjacent lines next time. But as I found these lines are:
puts "We can also do that this way:"
puts "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_pont
sentence = "All god\tthings come to those who weight."
words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)
From here we see that the line before your specified line doesn't have closing parenthesis ')'.
My users sometimes enter, instead of apostrophe ('), symbols that look like apostrophe (‘), which causes some problems with database.
I tried to replace them with gsub like so:
result.gsub(/\‘/, "'")
result.gsub(/‘/, "'")
Neither of these options work - getting the error:
syntax error, unexpected $end, expecting ')'
return result.gsub(/\‘/, "'").gsub("’", "'")
^
Are they reserved by Ruby? How do I replace them?
If your text editor doesn't support UTF-8 characters like ‘ directly, you can escape them this way:
"\u2018"
So in your example, it would be:
result.gsub(/\u2018/, "'")
Try:
result.gsub("‘", "'")
It should work.
In addition to what #kiplantt said, the following also works(just tested it)
puts result.gsub(/(\`)/, "\\'")
I am currently on Lesson 9 in "Learn Ruby the hard way".
I have typed the the line number 6 exactly as the way its being instructed but still I am getting error while executing.
It says:
Syntax error, unexpected tCONSTANT, expecting $end
puts " Here ^ are the days : ", days
You have forgotten to close a string on a previous line. Here's the problem reproduced:
paul#paulbookpro ~ ⸩ ruby
days = "abc
puts "Here are the days"
-:2: syntax error, unexpected tCONSTANT, expecting $end
puts "Here are the days"
^
It's treating the double-quote before the word "Here" as the closing quote of the string on the previous line, and then wondering why you're using a constant called Here (token beginning with upper case letter).
The error message means that the ruby parser encountered a constant (i.e. an identifier starting with a capital letter) where it did not expect one (specifically the parser expected the file to end at that point).
Since the code you've shown does not even contain a constant, the problem is likely caused by another part of your code.
I am currently using Watir with Firefox and it seems that when I try to set a field with the following text:
##$QWER7890uiop
The command I am using is the following:
text_field(:name, "password").value=("!##$QWER7890uiop)
I've also tried this:
text_field(:name, "password").set "!##$QWER7890uiop)
Only the first 2 characters get entered. Is there something I can do to by pass this feature?
You need to escape the string using single quotes '.
text_field(:name, "password").value='"!##$QWER7890uiop'
Many characters are substituted inside double quotes.
Escape sequences like \n, \t, \s, etc are replaced by their equivalent character(s). See here for full list.
#{} where anything the braces is interpreted as a ruby expression.
#$something where $something is interpreted as a ruby global variable. That's the problem with your quote above, beside not being terminated.
%s is interpreted as an ERB template expression (it is interpolated).
For instance:
puts "%s hours later" % 'Five'
results in
"Five hours later".
I'm trying to learn RegEx in Ruby, based on what I'm reading in "The Rails Way". But, even this simple example has me stumped. I can't tell if it is a typo or not:
text.gsub(/\s/, "-").gsub([^\W-], '').downcase
It seems to me that this would replace all spaces with -, then anywhere a string starts with a non letter or number followed by a dash, replace that with ''. But, using irb, it fails first on ^:
syntax error, unexpected '^', expecting ']'
If I take out the ^, it fails again on the W.
>> text = "I love spaces"
=> "I love spaces"
>> text.gsub(/\s/, "-").gsub(/[^\W-]/, '').downcase
=> "--"
Missing //
Although this makes a little more sense :-)
>> text.gsub(/\s/, "-").gsub(/([^\W-])/, '\1').downcase
=> "i-love-spaces"
And this is probably what is meant
>> text.gsub(/\s/, "-").gsub(/[^\w-]/, '').downcase
=> "i-love-spaces"
\W means "not a word"
\w means "a word"
The // generate a regexp object
/[^\W-]/.class
=> Regexp
Step 1: Add this to your bookmarks. Whenever I need to look up regexes, it's my first stop
Step 2: Let's walk through your code
text.gsub(/\s/, "-")
You're calling the gsub function, and giving it 2 parameters.
The first parameter is /\s/, which is ruby for "create a new regexp containing \s (the // are like special "" for regexes).
The second parameter is the string "-".
This will therefore replace all whitespace characters with hyphens. So far, so good.
.gsub([^\W-], '').downcase
Next you call gsub again, passing it 2 parameters.
The first parameter is [^\W-]. Because we didn't quote it in forward-slashes, ruby will literally try run that code. [] creates an array, then it tries to put ^\W- into the array, which is not valid code, so it breaks.
Changing it to /[^\W-]/ gives us a valid regex.
Looking at the regex, the [] says 'match any character in this group. The group contains \W (which means non-word character) and -, so the regex should match any non-word character, or any hyphen.
As the second thing you pass to gsub is an empty string, it should end up replacing all the non-word characters and hyphens with empty string (thereby stripping them out )
.downcase
Which just converts the string to lower case.
Hope this helps :-)
You forgot the slashes. It should be /[^\W-]/
Well, .gsub(/[^\W-]/,'') says replace anything that's a not word nor a - for nothing.
You probably want
>> text.gsub(/\s/, "-").gsub(/[^\w-]/, '').downcase
=> "i-love-spaces"
Lower case \w (\W is just the opposite)
The slashes are to say that the thing between them is a regular expression, much like quotes say the thing between them is a string.