I want to define an array in ruby in following manner
A = ["\"]
I am stuck here for hours now. Tried several possible combinations of single and double quotes, forward and backward slashes. Alas !!
I have seen this link as well : here But couldn't understand how to resolve my problem.
Apart from this what I need to do is - 1. Read a file character by character (which I managed to do !) 2. This file contains a "\\" character 3. I want to do something if my array A includes this backslash
A.includes?("\")
Any help appreciated !
There are some characters which are special and need to be escaped. Like when you define a string
str = " this is test string \
and this contains multiline data \
do you understand the backslash meaning here \
it is being used to denote the continuation of line"
In a string defined in a double quotes "", if you need to have a double quote how would you doo that? "\\"", this is why when you put a backslash in a string you are telling interpretor you are going to use some special characters and which are escaped by backslash. So when you read a "\\" from a file it will be read as "\\" this into a ruby string.
char = "\\"
char.length # => 1
I hope this helps ;)
您可以使用双引号中的反斜杠来转义反斜杠,例如:
["\\"].include?("\\")
Your issue is not with Array
, your question really involves escape sequences for special characters in strings. As the \\
character is special, you need to first prepend it (escape it) with a leading backslash, like so.
"\\"
You should also re-read your link and the section on escape sequences .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.