Reputation: 668
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 !
Upvotes: 2
Views: 1187
Reputation: 579
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 ;)
Upvotes: 5
Reputation: 3907
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.
Upvotes: 2
Reputation: 4777
You can escape backslash with a backslash in double quotes like:
["\\"].include?("\\")
Upvotes: 1