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
[Ruby] Quote(') and double quote(") explained
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 expectedThe 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
ewulamideTwo 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\tulamideVery 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\tulamideBut this the uncomfortable way, so I recommend single quotes instead.
Re: [Ruby] Quote(') and double quote(") explained
I do hope there'll be more to come.
Cheers
Spogg
Re: [Ruby] Quote(') and double quote(") explained
-
KG_is_back
- Posts: 1196
- Joined: Tue Oct 22, 2013 5:43 pm
- Location: Slovakia
Re: [Ruby] Quote(') and double quote(") explained
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"
Re: [Ruby] Quote(') and double quote(") explained
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!