Is this a ruby bug?
-
I'm creating a cross brace ruby, with left or right bracing options
I wanted to use the keyboard "" to define the brace direction in the dialog box
-
Within double-quotes [""] in Ruby the back-slash \ has a special-power - it's the 'escape-character' - giving different meanings to some characters that might follow it.
So to make a \ you must type "\".
It's also used to make a " - as "1"" [making 1"]; or a new-line in a block of text - as "\n"; or a tab to format text - as "\t" etc.
That's also why when typing PC paths in Ruby you must either use forward-slashes, or double-up your back-slashes, or use '' instead of ""...
"C:/Temp/my.skp"
"C:\Temp\my.skp"
'C:\Temp\my.skp'
The MAC always uses / anyway...This special-power for \ doesn't always work the same way inside single-quotes [''] - it only applies to \ to make a \ at the very end of a string, or ' to make a ' within a string; it does not apply to the other things like newline or tab that "" uses. So an alternative way of typing a \ could also be '\'
EDIT: Corrected an error in the '' section -
great! "\" works, thanks for the explanation.
-
@tig said:
As you see this special-power doesn't work the same way inside single-quotes [''] - so an alternative way of typing a \ would be ''
This is not correct.
'\' Error: #<SyntaxError: (eval):155: compile error (eval):155: unterminated string meets end of file> (eval):155
\
is always an escape character. Even within single quoted string - because you might want to insert a single quote character. So within single quoted string you still have these couple of escape sequences:\'
and\\
.You can use different delimiters if you want to mix ' and " in a string without escaping:
myString = %&This is "my" String&
But beware that if you want to include your delimiter you must escape it:
myString = %&This is "my" \& String&
More info:
http://www.techotopia.com/index.php/Ruby_Strings_-_Creation_and_Basics -
TT you are of course correct.
Within''
only\'
within a string and any final\\
are needed - for example\n
doesn't apply as a newline.
I've adjusted the original post to avoid further confusion
Advertisement