About Ruby single quotes and double quotes

Introduction

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.

Cause

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?

Premise

(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

Reason

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.

Summary

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

About Ruby single quotes and double quotes
About the difference between "(double quotation)" and "single quotation" in Ruby
About Ruby hashes and symbols
About Ruby and object model
About Ruby classes and instances
About Ruby product operator (&) and sum operator (|)
About object-oriented inheritance and about yield Ruby
About Ruby symbols
About ruby ​​form
About Ruby Hashes
[About double hash]
About Ruby arrays
[ruby] Double hash
About Ruby inheritance
About ruby block
About Ruby Hashes
About Ruby Symbols
About Ruby variables
Ruby and Gem
Ruby double hash
About Ruby methods
[Ruby] I thought about the difference between each_with_index and each.with_index
About Ruby error messages
[Ruby] Classes and instances
About Ruby exception handling
Symbols and Destructive Ruby
About Ruby Hashes (continued)
About eval in Ruby
[Ruby] Big Decimal and DECIMAL
About Bean and DI
[ruby] About here documents
About classes and instances
About Ruby if statement
A rough note about Ruby arrays and hash objects
About gets and gets.chomp
About Ruby instance methods
About redirect and forward
Ruby classes and instances
Ruby inheritance and delegation
About encapsulation and inheritance
Ruby variables and methods
[Ruby] About instance generation
[Technical memo] About the advantages and disadvantages of Ruby
About Serializable and serialVersionUID
Be careful about Ruby method calls and variable references
About the [ruby] operator
Thinking about logic Ruby
[Ruby] Questions and verification about the number of method arguments
[Ruby] About the difference between 2 dots and 3 dots of range object.
About for statement and if statement
About synchronized and Reentrant Lock
GraphQL Ruby and actual development
Explanation about Ruby Range object
[Ruby on Rails] about has_secure_password
About regular expressions in Ruby
Transactions, locks and double billing
[Java] About String and StringBuilder
About the same and equivalent
Ruby C extension and volatile
Summarize Ruby and Dependency Injection
Ruby About various iterative processes