[Ruby] Quote(') and double quote(") explained
Posted: Sun Apr 22, 2018 12:10 pm
Even as a beginner, you probably saw that you can use quotes or double quotes to assign a string:
That doesn't mean they're redundant. But their behaviour only differs in two situations. The first one happens, when the text you want to assign as a string contains a double quote or quote. You then need to use the other one to build a string, or else you get an error:
The other situations are escaped control signs. The ascii set contains control signs like new line, tab, return and others. Those can be used in strings to format them:
With single quotes, the characters are not interpreted, but taken as they are:
This is very handy, when you consider Windows. Say, you have a folder named music, containing a folder named rave, which contains a folder named new and lastly it contains a folder named tulamide. See what happens if you use double quotes:
Two lines of nonsense, that Windows will complain about. With single quotes it ends exactly as you need it:
Very comfortable! But to add to your confusion, there's a way to get to the correct result with double quotes as well. And if I wouldn't mention it, chances are the gurus will chop my head off.
You can escape from the escape! This is done by adding a \ to every escaped control char:
But this the uncomfortable way, so I recommend single quotes instead.
- Code: Select all
mystring = "Hello World"
mystring = 'Hello World'
That doesn't mean they're redundant. But their behaviour only differs in two situations. The first one happens, when the text you want to assign as a string contains a double quote or quote. You then need to use the other one to build a string, or else you get an error:
- Code: Select all
mystring = "Hello"World" #produces an error
mystring = 'Hello"World' #works as expected
mystring = 'Hello'World' #produces an error
mystring = "Hello'World" #works as expected
The other situations are escaped control signs. The ascii set contains control signs like new line, tab, return and others. Those can be used in strings to format them:
- Code: Select all
mystring = "Hello\nWorld" #will use seperate lines for Hello and World ( \n instructs a newline)
With single quotes, the characters are not interpreted, but taken as they are:
- Code: Select all
mystring = 'Hello\nWorld' #will just print Hello\nWorld, no new line is created.
This is very handy, when you consider Windows. Say, you have a folder named music, containing a folder named rave, which contains a folder named new and lastly it contains a folder named tulamide. See what happens if you use double quotes:
- Code: Select all
mystring = "C:\music\rave\new\tulamide"
##results in:
C:musicave
ewulamide
Two lines of nonsense, that Windows will complain about. With single quotes it ends exactly as you need it:
- Code: Select all
mystring = 'C:\music\rave\new\tulamide'
##results in:
C:\music\rave\new\tulamide
Very comfortable! But to add to your confusion, there's a way to get to the correct result with double quotes as well. And if I wouldn't mention it, chances are the gurus will chop my head off.
You can escape from the escape! This is done by adding a \ to every escaped control char:
- Code: Select all
C:\\music\\rave\\new\\tulamide
##results in:
C:\music\rave\new\tulamide
But this the uncomfortable way, so I recommend single quotes instead.