At the time of Rails integration test, when I was searching for elements using the find method, a syntax error occurred, so I summarized it as a memorandum.
When getting the selector in the integration test, SyntaxError occurs if you write as follows.
find("input[name="commit"]").click
If you change it as follows
find('input[name="commit"]').click
SyntaxError has been resolved. Why did the Syntax Error occur?
(WIP) Expression expansion is possible with double quotes, but expression expansion is not possible with single quotes.
> number = 1234
=> 1234
> "090#{number}5678"
=> "09012345678"
Single quote
> number = 1234
=> 1234
> '090#{number}5678'
=> "090\#{number}5678"
#Expression expansion is not possible
There is a clear difference between double quotes and single quotes.
Backslash notation is valid for double quotes.
puts "Line breaks\n was done"
Line breaks
it was done
In single quote
puts 'No line breaks\was not n'
Line breaks\was not n
When the ruby interpreter (the one who parses the code) recognizes the starting double quote (single quote), it recognizes the next double quote (single quote) as closed.
"(← Start character string)"(← Character string closed) Character (← What is this and an error occurs.)
If you escape it, it will be recognized as part of the string instead of being closed, so let's escape it.
You need to escape it as follows:
Good example:
'\'hoge\''
"\"hoge\""
"'hoge'"
'"hoge"'
bad example:
""hoge""
''hoge''
By the way, if you check a pattern like this one with irb
> str = '"hoge"'
=> "\"hoge\""
It will escape as above. Therefore, it seems that the error could be avoided.
I thought that would be the case if I thought about it normally, but if you have similar questions, I would appreciate it if you could read it.
Recommended Posts