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

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

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.')

Ruby - How do I escape both a back to back backslash and a single

I have the following string, \'e4, which I can't seem to get printed to my IRB console.
My attempts:
"\'e4" gives me back "'e4"
"\\'e4" gives me back "\\'e4"
Any ideas?
Double-quoted strings will always show up with double backslashes in irb due to the way they're escaped.
If you want to see what it actually is:
puts "\\'e4"
# => \'e4
Your first attempt is read as "literal quote e 4" and the second is "literal backslash quote e 4". The second one is correct regardless of how it's displayed.

How can I pass special characters [eg. " or ( or ' ] as arguments to shell script?

I got one error while passing the arguments to outlook_DataParsing.sh:
$ sh outlook_DataParsing.sh delete node doc('/opt/ws40/contacts.xml')//Directory/Contacts/Contact[#id='22222']
and I am reading all the arguments as:
str=$#
The error is following:
-bash: syntax error near unexpected token `('
Can anybody help me?
There are a number of "special" characters in a shell command, including $()[]
Most of these can simply be passed by enclosing the parameter in double quotes
foo "(hello)[]"
This however will not fix the $ sign, as it is intended for variables. You can instead use single quotes to pass a $ sign
foo '$im_not_a_variable'
If all else fails, ANY character can be escaped with a backslash \ including a space (no quotes needed)
foo \(hello\)\[\]\ \$im_not_a_variable

Bash quoting problem

A="echo 'q'"
$A
I got the result 'q'
but if I type echo 'q' directly, it's q (without single quote)
So, I wonder what rule does bash follow while facing single quotes inside the double quotes.
The original problem is
A="curl http://123.196.125.62/send -H 'Host: poj.org' -e http://poj.org/send"
$A
I got curl: (6) Couldn't resolve host 'poj.org''
it will be all right if I just type the command into the terminal..
P.S. I'd like to use $A for excuting the command inside A
Please see BashFAQ/050: I'm trying to put a command in a variable, but the complex cases always fail!
It's best to avoid putting commands in variables for the reason you've experienced, among others.
You should use a function and pass it arguments.
Why do you want to do this rather than simply executing the command directly?
If you must do it, use an array:
A=(curl http://123.196.125.62/send -H 'Host: poj.org' -e http://poj.org/send)
${A[#]}
Regarding the treatment of single quotes within double quotes, they are treated literally and as part of the rest of the string. Here is the relevant paragraph from man bash:
Enclosing characters in double quotes preserves the literal value of
all characters within the quotes, with the exception of $, `, \, and,
when history expansion is enabled, !. The characters $ and ` retain
their special meaning within double quotes. The backslash retains its
special meaning only when followed by one of the following characters:
$, `, ", \, or <newline>. A double quote may be quoted within double
quotes by preceding it with a backslash. If enabled, history expansion
will be performed unless an ! appearing in double quotes is escaped
using a backslash. The backslash preceding the ! is not removed.
If you want to "save" a command for later execution, you do NOT want a variable. You want a function.
a() { curl http://123.196.125.62/send -H 'Host: poj.org' -e http://poj.org/send; }
Putting code in variables is bad since variables are containers for data not code. Additionally, you are seeing the problem because $A does NOT execute the bash code in A, what it really does is split the value of A into words, then it performs Pathname Expansion on these words, and as a result of those two operations, it executes a program named by the first resulting word and passes the other words as arguments. In your particular case, this is what happens (I use [] to indicate "units"):
A: [echo 'q']
after wordsplitting: [echo] ['q']
after pathname expansion: [echo] ['q']
Now bash looks for a program called echo and passes the argument 'q' to it.
This is NOT executing bash code, because if you execute echo 'q' as bash code, bash removes the single quotes after it's done with them. Similarly, you cannot do pipes, redirection et al. like this, because they too are bash syntax (just like your single quotes).
Recap: never put code in bash variables. never leave parameters unquoted (if you think doing that fixes something, you are wrong, you've just made things worse, go fix the real problem). The solution is to use a function.
You'd better use backquotes in this case:
A=`curl http://123.196.125.62/send -H 'Host: poj.org' -e 'http://poj.org/send'`

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

Resources