Replace Single Quotes with Apostrophe in String (Ruby) - ruby

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(/(\`)/, "\\'")

Related

Is there a way I can print a statement with an exclamation mark and a quotation mark?

I'm trying to print a statement that looks like this: "Oh no!",
but I keep getting: Oh no! without the quotes.
This is the code I've been using:
print(exclamation.capitalize() + ("!") , "I yelled" + ".")
Please what am I missing?
In Python 3.6+, you can use f-strings to embed statements directly in the string.
Also, if you use single quotes to capture the string, you can use double-quotes in the string and they will print without needing to be escaped.
print(f'"{exclamation.capitalize()}"! I yelled.')

Error - RSpec - expect method is escaping '\n'

I would like to assert this expectation:
contas_csv = transacoes_decorator.converter_contas_para_csv
expect(contas_csv).to eq('345,30000\n350,19500\n355,20000\n360,-31000\n')
I got this error:
expected: "345,30000\\n350,19500\\n355,20000\\n360,-31000\\n"
got: "345,30000\n350,19500\n355,20000\n360,-31000\n"
I printed the contas_csv variable, and I got what I expected "345,30000\n350,19500\n355,20000\n360,-31000\n"
But the RSpec is adding an extra '\' before the '\n'. Because of it, my test is failing.
Someone can help me with that, please?
You're using single quotes so special characters like \n are taken literally, not expanded to their control-character counterparts.
Use this:
"...\n..."
With double quotes. '\n' is the same as "\\n".

using .gsub to double quotes and punctuation in Ruby

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.

Watir magic escape sequence?

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

in Ruby, trying to convert those weird quotes into "regular" quotes

I am trying to parse a text file that has the weird quotes like
“ and ” into "normal quotes like "
I tried this:
text.gsub!("“",'"')
text.gsub!("”",'"')
but when it's done, they are still there and show up as
\x93 and \x94
so I tried adding that too with no luck:
text.gsub!('\\x93', '"')
text.gsub!('\\x94', '"')
The problem is, when I try to show those weird quotes on a webpage, it makes that weird diamond with a question mark symbol: �
It seems to work:
text = "“foo”"
=> "\342\200\234foo\342\200\235"
irb(main):002:0> text.gsub!("“",'"')
=> "\"foo\342\200\235"
irb(main):003:0> text.gsub!("”",'"')
=> "\"foo\""
You need to use a hex editor to figure out all the character codes involved.
Re: the second question of why the weird quotes show on a web page as the � symbol:
Your problem is that your web page is not in UTF-8 mode. To get it there, see
http://www.w3.org/International/O-HTTP-charset
If you can't change your web server, add a meta line in the head section of your web pages: http://www.utf-8.com/
Larry
Your first gsubs should work. The reason the second set of gsubs don't work is that you're using single quotes and double backslash. Try the other way around:
text.gsub!("\x93", '"')
text.gsub!("\x94", '"')
You can also do this in one line:
text.gsub!("\x93", '"').gsub!("\x94", '"')
# or
text.gsub!(/(\x93|\x94)/, '"')
Are you sure the encoding of the string is correct?

Resources