If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
[Ruby] Quote(') and double quote(") explained
5 posts
• Page 1 of 1
[Ruby] Quote(') and double quote(") explained
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.
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: [Ruby] Quote(') and double quote(") explained
I love these mini tutorials you do!
I do hope there'll be more to come.
Cheers
Spogg
I do hope there'll be more to come.
Cheers
Spogg
-
Spogg - Posts: 3358
- Joined: Thu Nov 20, 2014 4:24 pm
- Location: Birmingham, England
Re: [Ruby] Quote(') and double quote(") explained
If I may QUOTE Spogg .... love em. Thanks T.
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
Re: [Ruby] Quote(') and double quote(") explained
There is also another very important difference. Double quote strings allow string interpolation using special #{} command, that allows you to insert data into string:
You can get identical result by appending strings, but that's slower and slightly harder to read:
- Code: Select all
h=182
m=67.684
wh="Height: #{ h.to_s}cm Weight: #{m.round(1).to_s}kg" #-> "Height: 182cm Weight: 67.7kg"
bmi="Body Mass Index: #{ (10000*m/(h*h)).round(3).to_s }" #-> "Body Mass Index: 20.434"
You can get identical result by appending strings, but that's slower and slightly harder to read:
- Code: Select all
h=182
m=67.684
wh="Height: "+ h.to_s+"cm Weight: "+m.round(1).to_s+"kg" #-> "Height: 182cm Weight: 67.7kg"
bmi="Body Mass Index: "+ (10000*m/(h*h)).round(3).to_s #-> "Body Mass Index: 20.434"
- KG_is_back
- Posts: 1196
- Joined: Tue Oct 22, 2013 5:43 pm
- Location: Slovakia
Re: [Ruby] Quote(') and double quote(") explained
@Spogg & RJ
Whenever I spot something, that I think is not too complicated, I will do another post. Thank you!
@KG
Good point! I too use #{} a lot nowadays. In the beginning I did it the hard way of adding strings, just as I was used to from older languages. But #{} is such a very nice shortcut, whose functionality is present in other modern languages as well, so I guess it was inspired by them. Just a little correction: You don't need ::to_s. It is called automatically when the variable doesn't represent a string. "... #{h} cm ..." is sufficient.
Yes, the use of #{} is only possible in strings with double quotes, guys!
Whenever I spot something, that I think is not too complicated, I will do another post. Thank you!
@KG
Good point! I too use #{} a lot nowadays. In the beginning I did it the hard way of adding strings, just as I was used to from older languages. But #{} is such a very nice shortcut, whose functionality is present in other modern languages as well, so I guess it was inspired by them. Just a little correction: You don't need ::to_s. It is called automatically when the variable doesn't represent a string. "... #{h} cm ..." is sufficient.
Yes, the use of #{} is only possible in strings with double quotes, guys!
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 32 guests